初入云开发 异步的问题
发布于 6 年前 作者 fujing 8428 次浏览 来自 问答
  • 需求的场景描述(希望解决的问题)下面是原来的代码 自己试着捣鼓 async await 无果 特来求大神助
  • 希望提供的能力

 doUpload: function() { // 选择图片

   var that = this;

   const filePath = that.data.images,

     var imgFileId = [];

   var cloudPath = [];

   filePath.forEach((item, i) => {

     cloudPath.push(util.formatTimestamp(new Date()) + filePath[i].match(/\.[^.]+?$/)[0])

   })

   filePath.forEach((item, i) => {

     wx.cloud.uploadFile({

       cloudPath: cloudPath[i],

       filePath: item, // 文件路径

     }).then(res => {

       console.log(res)

       imgFileId.push(res.fileID)

       console.log(imgFileId)

     })

   })

   return imgFileId;

 },

 submitForm: function(e) {

   console.log(e)

   var that = this;

   let imgFileId = that.doUpload()

   console.log(imgFileId)

   //添加操作

   let pudate = new Date();

   const db = wx.cloud.database()

   db.collection(‘forum’).add({

     data: {

       content: e.detail.value.textarea,

       pubdate: util.formatTime(pudate),

       img: imgFileId,

       title: e.detail.value.userTitle,

       address: that.data.addressObj

     }

   }).then(res => {

     wx.showToast({

       title: “添加成功”

     })

   })

 },

1 回复

doUpload 里的遍历上传用Promise.all来改造一下即可

回到顶部