gpt4 book ai didi

javascript - Node .js + mysql : "Cannot enqueue Handshake after already enqueuing a Handshake."

转载 作者:行者123 更新时间:2023-11-29 10:01:04 25 4
gpt4 key购买 nike

我正在尝试创建两个函数,一个从 SQL 数据库检索对象,另一个将对象保存到同一个 SQL 数据库。我使用 node.jsmysql 来执行此操作。我有两个函数,fetchEmployeeEmployee.save,分别获取和保存员工。然而,当我调用 fetchEmployee 并且回调包含 Employee.save 时,我收到错误 Cannot enqueue Handshake after already enqueue Handshake. 即使更奇怪的是,Employee.save 似乎在抛出错误之前运行。

编辑:employee.save出现运行是异步的症状,因为console.log("Saved!")被调用在回调函数传递给SQL.parse之前,这意味着错误出现在parse期间。此外,如果在 parse 中,在 con.connect 之后添加 console.log("connection Created");,并且 console.log("Made it out."); 添加在con.connect末尾,调用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,因为数据与员工的数据一起正确记录到控制台。然后它会记录 Saved!,似乎表明 Employee.save 运行正确,然后在完成所有代码后,抛出错误。我一生都无法弄清楚为什么会发生这种情况,无论是在这里还是在谷歌上或通过测试。

我尝试将 con.end 添加到 sql.jsparse 的末尾,这会将错误更改为 调用退出后无法将握手入队

最佳答案

我能够通过放置来解决这个问题

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

parse 函数内部,尽管我不是 100% 确定为什么会这样。

关于javascript - Node .js + mysql : "Cannot enqueue Handshake after already enqueuing a Handshake.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52957041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com