收藏
回答

wx.uploadfile的回调中可以赋值也能打印,但出了success回调打印undefined?

我是做后端的,对前端懂一点,有没有大佬帮我指正一下,我在uploadfile的success中赋值打印没有问题,但是在回调成功外层在打印就拿不到值,let、var定义变量都试过,that=this调用setData也试过,但在回调外层都拿不到值,后台可以确认没有问题

这是代码

let imgtemp1 = '';
    wx.uploadFile({
      url:'http://localhost:10101/commission/commission/upPicture', 
      filePath: imagepath,
      name'file',
      formData: {
        'user''test'
      },
      successfunction (res{
        console.log(res,"上传图片结果");
        imgtemp1 = res.data
        console.log(imgtemp1);
        // _that.setData({
        //   imgtemp:imgtemp1
        // })
        // _that.imgtemp = res.data
        // console.log(_that.imgtemp);
      },
      failfunction(err){
        console.log(err,"上传文件出错");
      }
    })
    console.log(imgtemp1);


这是打印

other.js:347 
other.js:413 {data: true, header: {…}, statusCode: 200, cookies: Array(0), errMsg: "request:ok"} "asdasdasdasdasdsa"
other.js:334 {statusCode: 200, data: "http://172.21.xx.xx/2024031973.png", header: {…}, cookies: Array(0), errMsg: "uploadFile:ok"} "上传图片结果"
other.js:336 http://172.21.xx.xx/2024031973.png


回答关注问题邀请回答
收藏

2 个回答

  • 那一抹笑😃 穿透阳光
    那一抹笑😃 穿透阳光
    03-19

    你要异步改成同步,最后一个才能这么输出数据,不然就只有第一个输出有数据

    03-19
    有用 1
    回复
  • hello world
    hello world
    03-19

    回调外层肯定拿不到值啊,

    03-19
    有用
    回复 2
    • Super Mario
      Super Mario
      03-19
      啊?大佬那我应该赋值给谁然后作为参数传到后台啊,而且我也是在外层定义变量的啊
      03-19
      回复
    • hello world
      hello world
      03-19回复Super Mario
      只能在success回调里面处理逻辑了,或者封装成promise
      03-19
      1
      回复
登录 后发表内容