gpt4 book ai didi

javascript - Node.js Promise 代码在一个实例中有效,但在另一个实例中无效

转载 作者:行者123 更新时间:2023-11-29 18:19:24 25 4
gpt4 key购买 nike

我有一个node.js 服务器端代码,它对 mysql 执行一些基本的数据库操作。我使用 mysql2-promise 作为库。

功能布局:

  • contacts_GET(contactid) => getContactDetails();
  • contacts_GET(email) => getContactId(email) => getContactDetails(contactid);

函数实现:

  contacts_GET : function(args, context, cb){
if (args.pocid !== undefined ){
db.getContactDetails(args.pocid)
.then((results)=>{
var resp = {
"contacts" : results
};
cb(null, resp);
}).catch((reason)=>{
cb(JSON.stringify(errorHandler.throwInternalServerError(reason)));
});
}else if (args.email !== undefined ) {
db.getContactId(null, args.email)
.then((pocid)=>{
logger.debug("got back pocid=", pocid);
return db.getContactDetails(pocid);
}).then((results)=>{
logger.debug("got back result from getContactDetails=", results);
var resp = {
"contacts" : results
};
cb(null, responseObj(resp,args));
}).catch((reason)=>{
cb(JSON.stringify(errorHandler.throwInternalServerError(reason)));
});
}
}


getContactId : function(name, email, conn){
logger.debug("Inside getContactId. [name, email]", name, email);
if (conn === undefined ){
return this.mysql.createConnection(this.options)
.then((conn2)=>{
const b = this.getContactId(name, email, conn2);
conn2.end();
return b;
});
}else{
return conn.execute(`select pocid from Contacts where email = '${email}'`)
.then(([rows, fields])=>{
logger.debug("Inside getContactId. Result:", rows, fields);
if (rows.length == 0) {
return null;
}else{
return rows[0].pocid;
}
});
}
}

getContactDetails : function(pocid, conn){
logger.debug("Inside getContactDetails. [pocid, defined conn]", pocid, (conn!==undefined));
if (conn === undefined ){
return this.mysql.createConnection(this.options)
.then((conn2)=>{
const b = this.getContactDetails(pocid, conn2);
conn2.end();
return b;
});
}else{
const a = conn.execute("select name, email from Contacts where pocid=?", pocid)
.then(([rows, fields])=>{
logger.debug("Inside getContactDetails->cb. Result: ", rows);
if (rows.length == 0) {
return null;
}else{
return rows[0];
}
});
return a;
}
}

根据我的理解,then 返回的 Promise 可以用于链接,我已经为这两种情况编写了 Promise 链。

  1. 传入contactid(pocid)时,仅调用db.getContactDetails。在此函数中,它创建一个连接(返回一个 promise ),然后执行查询(这也返回一个 promise ),并解析和解析结果。此用例有效

  2. 当传入电子邮件(email)时,处理程序(contacts_GET)会创建一个 promise 链,从 getContactId 开始(返回一个链接连接创建、查询执行和结果解析的 promise ),链接到 getContactDetails (同上)。此用例失败

我的日志语句表明,在第二种情况下,在 getContactDetails 函数中从 conn.execute 返回 promise 后,执行突然停止。看起来 Promise 似乎从未被执行,因此 .then 部分(查询执行后)永远不会被调用。没有关于未处理的拒绝的错误或警告。此时的控制就消失了。

我很难理解为什么相同的函数在第一个用例中有效,但在第二个用例中失败。我已经阅读了 Promise 文档并阅读了每个有关边界场景的博客。我仍然对此感到茫然,希望得到所有帮助或指点。

谢谢!

最佳答案

结束这个问题。我发现问题不在于我的 Promise 代码,而在于我如何处理 Promise.catch 中捕获的原因。这导致底层 SQL 错误被忽略,从而导致我注意到的行为。

我还发现,node-mysql2 包处理查询和执行方法的方式完全不同。使用查询方法,对参数进行适当的转义和替换;而execute方法则不进行参数替换。

关于javascript - Node.js Promise 代码在一个实例中有效,但在另一个实例中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46725590/

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