【发布时间】:2020-04-19 19:23:49
【问题描述】:
console.log 所有sql查询后的查询结果如何?我正在使用mysql连接。现在单推(结果)发生在主console.log(keys)之后。我想要单个查询的缓冲结果,然后以其他方式使用它。
db.query(
`INSERT INTO orders VALUES (?, ?, (SELECT SUM(currentPrice) FROM games WHERE games.id in (?)), 'pending', "?")`,
[md5(Math.random() * 99999), email, ids, ids],
async (error, results) => {
if (error) {
console.log(error);
res
.status(400)
.json({ message: "При выполнении заказа возникла ошибка" });
}
if (results) {
const keys = [];
await ids.forEach(async (id) => {
await db.query(
"SELECT * FROM gameworld.keys WHERE gameId = ? and used = 0 LIMIT 1",
id,
async (error, results) => {
if (error) {
console.log(error);
}
if (results) {
console.log(results);
await keys.push(results[0]);
await db.query(
"UPDATE gameworld.keys SET used = 1 WHERE id = ?",
results[0].id
);
}
}
);
});
await db.query(
"UPDATE order SET status = 'delivered' WHERE email = ?",
email
);
await console.log(keys);
【问题讨论】:
-
在缓冲区中添加结果变量,然后在控制台中显示缓冲区时出现什么问题? (我想你做到了)。您的问题是等待所有查询完成吗?
-
是的。我只想等待所有查询的结果,然后是 console.log 缓冲区键。但是现在最后一个 console.log 不等待 sql 查询的结果
标签: javascript mysql node.js asynchronous