多表更新怎么写?为什么会报错
发布于 5 年前 作者 lili 4656 次浏览 来自 问答
  • 需求的场景描述(希望解决的问题)

发布产品时需要消耗个人积分,所以需要在个人表中更新,扣除积分成功才会在清单表中记录,否则删除刚才的产品发布记录

let $result = {
  bool: false,
  message: '提交失败!'
};
 
try{
  await db.collection('Goods').add({
    data: e.iData
  }).then(res => {
    await db.collection('Users').doc(id).update({
      data: {
        _goods: _.unshift(res._id),
        _scores: _.inc(-$cost)
      }
    }).then(res => {
        await db.collection('Score').doc(id).update({
        data: {
          _history: _.unshift({
            _type: '支出',
            _scores: -$cost,
            _date: db.serverDate()
          })
        }
      }).then(res => {
        $result.bool = true;
        $result.message = '提交成功!';
      })
    }).catch(err => {
      await db.collection('Goods').doc(res._id).remove();
    })
  });
}catch(err){
  console.log(err);
}finally{
  return $result;
}


  • 希望提供的能力

怎么样写才不会报错?

3 回复

你的问题是这样的,async只能修饰主函数,并不能修饰回调函数 ,你下面每一次使用await的时候都是对回调函数而言的,但是你没有对回调函数进行async声明导致编译器不能识别await 你可以这样写 async res=>{

await

}

这样就可以调用了

你能说的清楚点吗?是不是不能这样嵌套?回滚又是怎么回事?

云函数里面写下 写错了然后回滚。现在没事务所以得自己记录失败

回到顶部