关于云函数使用tcbRouter后async/await与Promise.all() 一起用的问题?
发布于 5 年前 作者 xia69 2961 次浏览 来自 官方Issues

改了很多次,一直再报这个错{“errorCode”:1,“errorMessage”:“user code exception caught”,“stackTrace”:“Unexpected identifier”},请大家帮我看看。

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const tcbRouter = require('tcb-router')
const db = cloud.database()

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  //event中包含前端调用传过来的收藏状态、当前物品收藏信息
  const app = new tcbRouter({
    event
  })

  //在收藏列表页增删收藏记录,同时更新物品收藏数量
  app.router('cancelOrAgainShouCang', async(ctx, next) => {
    let cancelShouCangStatus = ''  //保存取消收藏是否成功
    let againShouCangStatus = ''   //保存再次收藏是否成功
    let promiseArray=[]  //存放promise对象的数组
    let p1,p2 

    //当前是已收藏状态,进行取消收藏逻辑操作,将对收藏集合、物品信息集合的数据库操作封装为promise对象
    if (event.shouCangStatus) {
       p1=new Promise((resolve,reject)=>{
       db.collection('shouCang-users').where({
          _openid: wxContext.OPENID,
          goodsId: event.currentShouCangInfo.goodsId
        }).remove().then((res)=>{
          resolve('deleteShouCangRecordSuccess')
        }).catch((err)=>{
          reject()
        })
      })

      p2=new Promise((resolve,reject)=>{
        db.collection('publish-goods-info').where({
          _id: event.currentShouCangInfo.goodsId
        }).update({
          data:{
            shouCangCounts: db.command.inc(-1)
          }
        }).then((res)=>{
          resolve('decreaseShouCangCountsSuccess')
        }).catch((err)=>{
          reject()
        })

        //使用Promise.all来执行之前封装的两个promise对象,返回执行成功的状态信息
        promiseArray=[p1,p2]
        cancelShouCangStatus=await Promise.all(promiseArray).then((res)=>{
          return 'cancelShouCangSuccess'
        }).catch((err) => {
          console.log(err)
        })
      })
    } 
    
    //当前是未收藏状态,进行再次收藏逻辑操作,将对收藏集合、物品信息集合的数据库操作封装为promise对象
    else {  
      p1=new Promise((resolve,reject)=>{
        db.collection('shouCang-users').add({
          data: {
            _openid: event.currentShouCangInfo._openid,
            goodsId: event.currentShouCangInfo.goodsId,
            shouCangTime: db.serverDate(),
            totalCategory: event.currentShouCangInfo.totalCategory,
            shouCangOfGoodsInfo: event.currentShouCangInfo.shouCangOfGoodsInfo
          }
        }).then((res) => {
          resolve('addShouCangCountsSuccess')
        }).catch((err)=>{
          reject()
        })
      })

      p2=new Promise((resolve,reject)=>{
        db.collection('publish-goods-info').where({
          _id: event.currentShouCangInfo.goodsId
        }).update({
          data:{
            shouCangCounts: db.command.inc(1)
          }
        }).then((res)=>{
          resolve('increaseShouCangCountsSuccess')
        }).catch((err)=>{
          reject()
        })
      })

      //使用Promise.all来执行之前封装的两个promise对象,返回执行成功的状态信息
      promiseArray=[p1,p2]
      againShouCangStatus=await Promise.all(promiseArray).then((res)=>{
        return 'againShouCangSuccess'
      }).catch((err)=>{
        console.log(err)
      })
    }

    //返回以上操作是否成功的状态信息
    ctx.body={
      cancelShouCangStatus,
      againShouCangStatus
    }
  })

  return app.serve()
}
1 回复

弃用tcbRouter。

回到顶部