node.js + mysql:“已将握手入队后无法将其入队”。

时间:2018-10-23 20:03:56

标签: javascript mysql node.js runtime-error

我试图做两个函数,一个函数从SQL数据库中检索对象,另一个函数将对象保存到相同的SQL数据库中。我正在使用node.jsmysql来做到这一点。我有两个函数,fetchEmployeeEmployee.save,分别获取和保存一名员工。但是,当我调用fetchEmployee并在回调中包含Employee.save时,我会收到错误Cannot enqueue Handshake after already enqueuing a Handshake.甚至很奇怪,Employee.save似乎在抛出错误之前就已运行。

编辑employee.save似乎是异步的症状,因为在回调函数传递给console.log("Saved!")之前调用了SQL.parse,这意味着出现了错误在parse期间。此外,如果在解析中在console.log("connection created");之后添加con.connect,并且在console.log("Made it out.");结束之后添加con.connect,则在调用Employee.save时,控制台将输出{{ 1}},然后引发错误,这意味着保存查询从未完成,但是在> "connection created"

之后引发了错误

Employee类由以下内容定义

con.connect

请注意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

最后,在此文件中定义了SQL.parse:

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
             })
         );
     });
}

当我调用这段代码时

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,似乎表明Saved!运行正确,然后在完成所有代码之后,抛出错误。我一辈子都无法弄清楚为什么会发生这种情况,无论是在这里还是在Google上,还是通过测试。

我尝试将Employee.save的{​​{1}}添加到con.end的末尾,这会将错误更改为parse

1 个答案:

答案 0 :(得分:0)

我能够通过放置

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

parse函数内部,尽管我不确定100%为何可行。