我不明白为什么将“ 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
    }
})


谢谢

最佳答案

这是异步功能,请在其中登录:

ProductM.findOne({name:"not available name"}).exec((err, product) => {
    productsAvailable=false
    console.log(productsAvailable)
    // you probably need to send response here
})

10-08 04:03