云函数wx.cloud.uploadfile上传图片成功但在这个函数体的任何赋值操作最后调用都无效?
发布于 4 年前 作者 hdeng 2702 次浏览 来自 官方Issues

先举个例子

let a=0;
page({
wx.uploadfile({
cloudPath ,//假设有值
filePath ,//假设有值
success:res=>{
a=1;
console.log(a);//在这里a=1;
}
})
consloe.log(a);//在这里a=0;相当于赋值操作无效
})

源代码展示

  
 uploadCloudImage:function(){
    let that=this;
    let userInfo=app.globalData.userInfo;
    let username=userInfo.nickName;
    let Time="",uploadImage=[];//uploadImage临时变量
    let img_url=that.data.img_url,filePath,cloudPath;//img_url便是要赋值的图片数组
    for(let i=0;i<img_url.length;i++){
      Time=utils.formatTime(new Date());
      filePath = img_url[i];
     cloudPath = username+`广场my-image`+`/`+Time+`${filePath.match(/\.[^.]+?$/)[0]}`;
       wx.cloud.uploadFile({
        cloudPath ,
        filePath ,
        successres => {
          console.log('[上传文件] 成功:', res);
          img_url[i]=res.fileID;//赋值
          that.setData({
            img_url:img_url,//赋值给页面数据
          })
          console.log(img_url);
          uploadImage.push(res.fileID);
        },
        faile => {
          console.error('[上传文件] 失败:', e)
          wx.showToast({
            icon'none',
            title'上传失败',
          })
        },
        complete() => {
          wx.hideLoading();
        }
      })
    }
    console.log(that.data.img_url);
  },
2 回复
必须是0
了解一下 promise asyncawait

试试在 success 上面 let that = this ;

回到顶部