正常186行应该和179行输出同一个值,结果却是
我要怎么把结果传给这个变量呢?
呆狗的一生 - 呆狗的一生
你逻辑不清晰,js语法不熟悉。then方法是基本方法,要会用。promise是异步的,不用说,能明白吧?先执行186行的输出,这时没有赋值,输出undefined,再执行178行的赋值,再执行179行输出。怎么可能是你所说的输出一样的值呢?你如果有额外的逻辑要写可以就添加到then方法里,也可以在catch之前额外再添加一个then,写在里面就可以了。
另外,await只能在异步方法里使用(添加了async
的方法)。如果你要使用await此类方式,可以采用如下写法:
async 方法名(){
let order = await db.collection('open-order').where({good_id:this.cart_data[i].goods_id[0]._id}).get();
let a = Object.assign( {} ,order.result.data)
this.gid = a[0].goods_id[i]
console.log( this.gid )
}
呆狗的一生 - 呆狗的一生
比如,在vue中
methods:{
async hello(){
let obj = await db.collection('user').doc("XXX").get()
console.log(`hello`,obj.name)
}
}
1***@qq.com (作者)
await db.collection("open-shopping-cart").doc(item._id).remove()
使用await还会报一下错误:
Cannot use keyword 'await' outside an async function
1***@qq.com (作者)
我是初学者,所以好多东西是一知半解,看官方文档都是直接await db.collection,所以我就用了。
额外的逻辑可以就添加到then方法里,所以我这个不算在then里面吗?
我知道要用async的方法,但是我不知道放到什么位置上,也是基础不牢的原因,我可以直接在我现在写逻辑的地方写async方法吗?
2022-09-24 16:36
呆狗的一生
回复 1***@qq.com: 需要在哪个方法中用await就在此方法名前加async
2022-09-24 20:22