WeChat applet reported an error: TypeError: Cannot read property'setData' of undefined and two solutions

WeChat applet reported an error: TypeError: Cannot read property'setData' of undefined

When completing a function of the WeChat applet, an error was reported occasionally.
The error message is: TypeError: Cannot read property'setData' of undefined. Share with you a description of the relevant situation and how to solve it

Error screenshot

Insert picture description here

Error code

Whenever I execute the homework function, I send a request to store the acquired data in a local array. So I used this.setData({}) in sucess:function(res){}.

  homework: function () {
    
    
    if (this.data.homeworkpageFlag == false) {
    
    
      console.log(this)
      this.setData({
    
    
        homeworkpageFlag: true,
        saypageFlag: false,
        exampageFlag: false,
      })
    }
    wx.request({
    
    
      url: '某url hhhhh',
      data: {
    
    
        student_id: '某学号',
      },
      method: 'POST',
      header: {
    
    
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
    
    
        console.log(res.data)
        this.setData({
    
    
          homeworkNoCompleteList: res.data.data.homeworkNoCompleteList
        })
      }
    })

  },

The ideal is very good, but every time it is executed, the above error will be reported.

solution

  1. var that = this;
  2. ES6 arrow functions

Option one, var that = this;

Add var that = this; and change this.setData in sucess to that.setData

homework: function () {
    
    
    if (this.data.homeworkpageFlag == false) {
    
    
      console.log(this)
      this.setData({
    
    
        homeworkpageFlag: true,
        saypageFlag: false,
        exampageFlag: false,
      })
    }
    //添加var that = this
    var that = this;
    wx.request({
    
    
      url: 'https://fanzaixuexi.applinzi.com/public/index.php/index/GetInformation/get_all_assignment',
      data: {
    
    
        student_id: '某学号',
      },
      method: 'POST',
      header: {
    
    
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
    
    
        console.log(res.data)
        //改成了that.setData
        that.setData({
    
    
          homeworkNoCompleteList: res.data.data.homeworkNoCompleteList
        })
      }
    })
  },

Solve the problem successfully.

Solution two, ES6 arrow function;


  homework: function () {
    
    
    if (this.data.homeworkpageFlag == false) {
    
    
      console.log(this)
      this.setData({
    
    
        homeworkpageFlag: true,
        saypageFlag: false,
        exampageFlag: false,
      })
    }
    wx.request({
    
    
      url: '某url',
      data: {
    
    
        student_id: '某id',
      },
      method: 'POST',
      header: {
    
    
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: (res)=> {
    
    
        console.log(res.data)
        // var that = this;
        this.setData({
    
    
           // 我要获取的数据
        })
        console.log(this.data.homeworkYesCompleteList.length)
      }
    })

  },

Change sucess:function(res)() to sucess:(res)=>()
to use this.setData generously

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/113706520