node.js+mysql:“在握手已经排队之后,不能排队握手”

vlf7wbxs  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(443)

我尝试创建两个函数,一个从sql数据库中检索对象,另一个将对象保存到同一个sql数据库。我正在使用 node.js 以及 mysql 做这个。我有两个功能, fetchEmployee 以及 Employee.save ,分别获取和保存一个雇员。当我打电话的时候 fetchEmployee ,回调包括 Employee.save ,但是,我得到了错误 Cannot enqueue Handshake after already enqueuing a Handshake. 更奇怪的是, Employee.save 似乎在抛出错误之前运行。
编辑: employee.save 运行是异步的一个症状,如 console.log("Saved!") 在回调函数传递给之前调用 SQL.parse 这意味着错误出现在 parse . 此外,如果在parse中 console.log("connection created"); 在后面添加 con.connect ,和 console.log("Made it out."); 在结束后添加 con.connect ,呼叫时 Employee.save ,控制台输出 > "connection created" ,然后抛出错误,这意味着save查询永远不会完成,但在完成之后会抛出错误 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 ,因为数据与员工的数据一起正确地记录到控制台。然后记录下来 Saved! ,似乎表明 Employee.save 正确运行,然后在所有代码完成后,抛出错误。我一辈子都搞不懂为什么会这样,在这里,在谷歌上,或者通过测试。
我试着补充 con.end 到年底 parsesql.js ,这会将错误更改为 Cannot enqueue Handshake after invoking quit

dauxcl2d

dauxcl2d1#

我通过放置

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

内部 parse 功能,但我不确定为什么会这样。

相关问题