3 回答
TA贡献1936条经验 获得超6个赞
var queryString = "SELECT price FROM menu_items WHERE id = " +
req.query.items + ";";
mysql.pool.query(queryString, function(err, rows, fields){
if (err){
next(err);
return;
}
itemPrice = rows[0].price;
console.log("item price is2: " + itemPrice);
console.log("item price is: " + itemPrice);
});
任何网络调用都是异步运行的,因此一旦进行了 db 调用,下一行就开始执行。
使用异步等待
const {promisify} = require('util');
(async ()=> {
var queryString = "SELECT price FROM menu_items WHERE id = " +
req.query.items + ";";
const query = promisify( mysql.pool.query).bind(mysql.pool);
const result = await query(queryString)
if (!result) {
next(result);
return;
}
console.log(result)
itemPrice = result[0].price;
console.log("item price is2: " + itemPrice);
console.log("item price is: " + itemPrice);
})()
TA贡献1829条经验 获得超7个赞
you can do it using following method
export const getPriceValue = async () => {
var queryString = "SELECT price FROM menu_items WHERE id = " +
req.query.items + ";";
mysql.pool.query(queryString, function(err, rows, fields){
if (err){
return 0;
}
if(row) {
return(rows[0].price);
}
});
}
let priceValue = await getPriceValue();
console.log('price::', priceValue);
TA贡献1859条经验 获得超6个赞
var queryString = "SELECT price FROM menu_items WHERE id = " +
req.query.items + ";";
mysql.pool.query(queryString, function(err, rows, fields){
if (err){
next(err);
return;
}
itemPrice = rows[0].price;
console.log("item price is2: " + itemPrice);
console.log("item price is: " + itemPrice);
});
添加回答
举报