全局变量在then内赋值后在函数外调用内容为空?

发布于 6 年前作者 min9010138 次浏览最后编辑 6 年前来自 issues

var dm

db.collection(‘zhongdui’).where({

_openid: this.data.openid

}).get().then(res => {dm=res.data[0],console.log(dm)})  //打印结果为{duiming: {…}}

console.log(dm)   //打印结果为undefined

3 回复
ping38
ping381 楼6 年前

如果不为空,才是出错了。

hgao
hgao2 楼6 年前

xxxx: async function(){

    var res = await db.collection(…)…get(),

    dm = res.data[0]

    console.log(dm)

}

ming50
ming503 楼5 年前

同步异步问题