收藏
回答

for循环总是再if之后才执行?

//临时图地址换cloud地址
  getCloudPath(path) {
    let result = 0
    wx.cloud.uploadFile({
      cloudPath'image/' + new Date().getTime() + "_" + Math.floor(Math.random() * 1000) + ".jpg",
      filePath: path,
      successres => {
        console.log("upload success", res.fileID)
        result = res.fileID
      }, failres => {
        result = 0
      }
    })
    return result
  },     


 that.editorCtx.getContents({
      success(res) {
        wx.showLoading({ title'发布中...', })
        var html = res.html
        if (res.text.trim()=="") {
          wx.hideLoading()
          that.setData({
            showOneButtonDialogtrue,
            test_content1'请添加文章内容'
          })
        } else {
          //临时图片地址更换云地址 --that.getCloudPath函数时利用临时地址换云存储地址
          for (var i = 0; i < that.data.imagelist_local.length; i++) {
            if (html.indexOf(that.data.imagelist_local[i]) >= 0) {
              let cloudpath = that.getCloudPath(that.data.imagelist_local[i])
              html.replace(that.data.imagelist_local[i], cloudpath)
            }
          }
          if(that.data.programid==''){//新发布
            db.collection('Program').add({
              data: {
                title: e.detail.value.title,
                html: html,
                content: res.text,
                datetime: util.formatTime(new Date()),
                creater: app.globalData.csridinfo.StaffName,
                id: that.data.index,
              },
              success(res) => {
                wx.showToast({
                  title'发布成功',
                })
              }, fail(res) => {
                wx.showToast({
                  title'发布失败',
                  icon'error'
                })
              }
            })
          }else{//修改
            db.collection('Program').doc(that.data.programid)
            .update({
              data:{
                title: e.detail.value.title,
                html: html,
                content: res.text,
                datetime: util.formatTime(new Date()),
                id: that.data.index,
              },success:(res)=>{
                wx.showToast({
                  title'发布成功',
                })   
              },fail:(res)=>{
                  wx.showToast({
                    title'发布失败',
                    icon'error'
                  })
              }
            })
          }
          
        }
      }
    })
回答关注问题邀请回答
收藏

2 个回答

  • 小黎
    小黎
    2022-12-01
    是因为你for循环里面getCloudPath方法里有异步请求
    


    2022-12-01
    有用
    回复
  • Mr.Zhao
    Mr.Zhao
    2022-12-01

    学学async await promise吧 代码执行顺序不是你想的样子 必学 社区高频问题

    2022-12-01
    有用
    回复
登录 后发表内容