需求:
要赋值的变量是 last_id
function foo() {
var last_id = 'AAAA'
// mongodb Model
ItemModel.find((err, items) => {
last_id = 'BBBB'
console.log(`LOG_1: ${last_id}`) // [结果正确]: BBB
})
console.log(`LOG_2: ${last_id}`) // [结果不是想要的]: AAA
}
问题:
- 如何解决?
- 可参考文档?
因为你的这段代码执行之前,
你的这段代码执行了
所以呢,你需要等第一步的代码执行完之后再执行最后的
console.log()
改成这样