WeChat applet calls the remote interface to the global array assignment code instance

The key is let this = this

Because this refers to the context of wx.Request in OnLoad, this should define let that = this

when the WX.Request’s context is referred to as ONLOAD.

After calling the remote service, after serialization, the OBJ assigns the error to Booklist

The error code is as follows:

  Page ({data: { BookList: []}, online: function () {wx.request ({URL: 'https://jiashubing.cn/wechat/book', header: {'content-type': 'Application / JSON'}, Success (rs) {var obj = json.parse (res. Data) console.log (obj) this.setdata ({Booklist: obj})}})}})  

being given as:

  VM3293: 1 thirdScriptErrorCannot read property 'setData' of null; at pages / index / index onLoad function; at api request success callback functionTypeError: Can not read Property 'setdata' of null  
The reason for the error is that this point points to problems, to save the instance at OnLoad, let this = this, the following full That, this point points to always worthy of its own context environment

The correct code is as follows:

Page ({Data: {Booklist: []}, online: function () {let that = this wx.request ({URL: ‘https://jiashubing.cn/ WeChat / book ‘, header: {‘ content-type ‘:’ Application / JSON ‘}, Success (RES) {var obj = json.parse (res. Data) Console.log ({Booklist: Obj}}}}}})
  The above is all the contents of this article, I hope to help everyone, I hope everyone supports TUMI clouds.  
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment