Error when uploading pictures in WeChat applet: ReferenceError: that is not defined

Wechat applet upload picture error

  • problem background

Recently, I am developing a WeChat mini-program short video project, which is currently developed to the user center module. However, when the avatar upload function is implemented, the avatar is uploaded successfully, but it cannot be successfully displayed.
insert image description here
ErrorReferenceError:that is not defined
insert image description here

  • Cause of the problem :

    This problem is quite simple. When sending an ajax request in a WeChat applet, sometimes the internal function cannot directly access the external this problem. This is because in the inner function, the context of this has changed. A common way to solve this problem is to use arrow functions (Arrow Function), arrow functions will not change the context of this. You can define the callback function of the ajax request using an arrow function, so that the callback function can access the external this. This is a JS problem (Vue also has this problem), if you don’t notice it all at once, record it, deepen your memory, or remind people who forgot like me

  • problem solved

    Define a global variable externally, and then assign the external this reference to the global variable, and then you can successfully access the
    insert image description heremodification, and you can see that the image is successfully uploaded and displayed
    insert image description here

Guess you like

Origin blog.csdn.net/qq_66345100/article/details/131716886