gpt4 book ai didi

MySQL Node JS .then() 不是函数

转载 作者:行者123 更新时间:2023-12-01 00:08:33 24 4
gpt4 key购买 nike

我不能使用 promise 进行 nodeJS mysql 查询吗?

// My DB settings
const db = require('../util/database');

db.query(sqlQuery, [param1, param2])
.then(result => {
console.log(result);
})
.catch(err => {
throw err;
});

返回:TypeError: db.query(...).then is not a function

最佳答案

您在评论中提到您希望等待查询 block 之后的逻辑,而不将该逻辑放在回调中。通过使用 Promise 包装方法,您可以这样做:

try {
const result = await new Promise((resolve, reject) => {
db.query(sqlQuery, (error, results, fields) => {
if (error) return reject(error);
return resolve(results);
});
});

//do stuff with result
} catch (err) {
//query threw an error
}

关于MySQL Node JS .then() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290574/

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