我不能使用 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/