gpt4 book ai didi

javascript - 变量不保留值

转载 作者:行者123 更新时间:2023-11-29 15:40:30 25 4
gpt4 key购买 nike

我尝试将变量设置为从查询获得的结果,但该变量不保留该值。

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);

我希望它打印“item Price is2:10.99”,然后“item Price is 10.99”,但它打印item Price is undefined 然后商品价格为2:10.99

最佳答案

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);
});

关于javascript - 变量不保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57721261/

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