Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js+;mysql:“;在已将握手排队后,无法将握手排队。”;_Javascript_Mysql_Node.js_Runtime Error - Fatal编程技术网

Javascript node.js+;mysql:“;在已将握手排队后,无法将握手排队。”;

Javascript node.js+;mysql:“;在已将握手排队后,无法将握手排队。”;,javascript,mysql,node.js,runtime-error,Javascript,Mysql,Node.js,Runtime Error,我试图创建两个函数,一个从SQL数据库检索对象,另一个将对象保存到同一个SQL数据库。我正在使用node.js和mysql来实现这一点。我有两个函数,fetchEmployee和Employee.save,分别用于提取和保存员工。当我调用fetchEmployee,并且回调包括Employee.save,但是,我得到一个错误在握手排队后无法排队。更奇怪的是,Employee.save似乎在抛出错误之前运行 EDIT:employee.save运行是异步的症状,因为在回调函数传递到SQL.pars

我试图创建两个函数,一个从SQL数据库检索对象,另一个将对象保存到同一个SQL数据库。我正在使用
node.js
mysql
来实现这一点。我有两个函数,
fetchEmployee
Employee.save
,分别用于提取和保存员工。当我调用
fetchEmployee
,并且回调包括
Employee.save
,但是,我得到一个错误
在握手排队后无法排队。
更奇怪的是,
Employee.save
似乎在抛出错误之前运行

EDIT
employee.save
运行是异步的症状,因为在回调函数传递到
SQL.parse
之前调用了
console.log(“Saved!”)
,这意味着在
parse
过程中会出现错误。此外,如果在parse
console.log中(“创建连接”)
添加在
con.connect
console.log(“makeit out.”)之后con.connect
结束后添加code>,当调用
Employee.save
时,控制台输出
>“connection created”
,然后抛出错误,这意味着保存查询从未完成,但在
con.connect
之后抛出错误

Employee类由以下内容定义

function Employee(obj) {
    /** Defines the Employee class
     * @arg obj.id : an integer; the employee's id
     * @arg obj.name : A string; the employee's name
     * @arg obj.position : A string; the employee's positions split by commas
     */

    this.id = obj.id;
    this.name = obj.name;
    this.position = obj.position;

    this.save = function() {
        SQL.parse({
            sql : `UPDATE EMPLOYEES
                SET id=?, name=?, position=?
                WHERE id=?`,
                replace_ : [this.id, this.name, this.position, this.id],
                result : false
        });
        console.log("Saved!");
    }
}
注意
console.log(“Saved!”),因为这将在稍后出现

fetchEmployee
由此函数定义:

function fetchEmployee(id, callback) {
    /** Fetch an employee from the employee table
     * @arg id : An integer; the id of the employee to fetch
     * @arg callback : A callback function to pass the employee to
     */

     SQL.parse({ // Perform the parse, define the sql, replace, and result
        sql : "SELECT * FROM employees WHERE id=?",
        replace_ : [id],
        result : true
     },

     function(err, data) {
         if(err) { // Pass an error if there's an error
             callback(err, null);
             throw err;
         }
         // Pass the employee to the callback as an employee object if there's no errors
         callback(null, new Employee({  // data is passed as a list from the sql table, so take only the object we need through [0]
                id : data[0].id,
                name : data[0].name,
                position : data[0].position
             })
         );
     });
}
最后,在该文件中定义了SQL.parse:

var mySQL = require("mysql");

var con = mySQL.createConnection({ //Create connection
    host : "localhost",
    database : "testdb1",
    user : "root",
    password : "***************" 
});

function parse(obj, callback) {
    /** Parses an sql query. 
     * @arg callback : A callback function, will be passed the data
     * @arg obj.sql : an sql query
     * @arg obj.replace_ : A list of replacements for ?s in sql
     * @arg obj.result : a boolean indicating whether a result should be returned
     */

     //Assign meaningfull values
     obj.replace_ = obj.replace_ || [];
     callback = callback || function() {};

    con.connect(function(err) {
        if(err) throw err;

        //Connect and pass the sql command to the server
        con.query(obj.sql, obj.replace_, function(err, data) {
            if(err) { //Pass the err to the callback if there is an err
                callback(err, null);
                throw err;
            }
            else if(obj.result) { // Pass the data to the callback if result is true
                callback(null, data)
            }
        });
    });
}

module.exports = {
    parse : parse
};
当我调用这段代码时

fetchEmployee(985, function(err, data) {
    if(err) throw err;
    console.log(data);
    data.save();
});
控制台输出

Employee {
  id: 985,
  name: 'Skidd',
  position: 'Dishwasher, Busser',
  save: [Function] }
Saved!
Error: Cannot enqueue Handshake after already enqueuing a Handshake. [...]
在我看来,它正确地运行了
fetchEmployee
,因为数据与员工的数据一起正确地记录到控制台。然后它记录
保存
,似乎表明
Employee.save
正确运行,然后在完成所有代码后抛出错误。我一辈子都不明白为什么会发生这种情况,在这里,在谷歌上,或者通过测试


我尝试在
sql.js
中的
parse
的末尾添加
con.end
,这将错误更改为
调用quit后无法排队握手

我可以通过放置

var con = mySQL.createConnection({ //Create connection
    host : "localhost",
    database : "testdb1",
    user : "root",
    password : "***************" 
});

parse
函数中,虽然我不能100%确定为什么这样做有效。

我可以通过放置

var con = mySQL.createConnection({ //Create connection
    host : "localhost",
    database : "testdb1",
    user : "root",
    password : "***************" 
});
parse
函数的内部,尽管我不能100%确定为什么这样做有效