[英]Can't set a variable inside mongoose callback function
I can't understand why "productsAvailable" show true after being set to false. 我不明白为什么将“ productsAvailable”设置为false后显示为true。
router.post('/api/transactions', (req, res) => {
var productsAvailable = true
for(var i=0; i<3; i++) {
ProductM.findOne({name:"not available name"}).exec((err, product) => {
productsAvailable=false //set to false
})
console.log(productsAvailable) //this show true
}
})
Thanks you 谢谢
This is async function, please log inside of it: 这是异步功能,请在其中登录:
ProductM.findOne({name:"not available name"}).exec((err, product) => {
productsAvailable=false
console.log(productsAvailable)
// you probably need to send response here
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.