uniapp develops WeChat applets stepping on the pit: TypeError: Cannot read property 'FormData' of undefined

Requirements: The endUniApp of has been in normal use, and now it is going to be compatible .H5微信小程序

1. Problem analysis

I thought it should be smooth, but I reported an error as soon as I came up, and I was very flustered. The error message is as follows:

页面[components/major/major-intro]错误:
 TypeError: Cannot read property 'FormData' of undefined
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:20109)
    at __webpack_require__ (runtime.js?t=wechat&s=1679123222779&v=c88fbffc60b4101fd05b6808eff1ad49:92)
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:20097)
    at __webpack_require__ (runtime.js?t=wechat&s=1679123222779&v=c88fbffc60b4101fd05b6808eff1ad49:92)
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:19751)
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:19976)
    at __webpack_require__ (runtime.js?t=wechat&s=1679123222779&v=c88fbffc60b4101fd05b6808eff1ad49:92)
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:19681)
    at __webpack_require__ (runtime.js?t=wechat&s=1679123222779&v=c88fbffc60b4101fd05b6808eff1ad49:92)
    at Object.<anonymous> (vendor.js?t=wechat&s=1679123222779&v=0a98776a18c77e11bdf2a8a15af44f38:19609)
(env: Windows,mp,1.06.2209190; lib: 2.30.2)

Click on the source code to see this:

/***/ }),
/* 170 */
/*!***********************************************!*\
  !*** ./node_modules/form-data/lib/browser.js ***!
  \***********************************************/
/*! no static exports found */
/***/ (function(module, exports) {
    
    

/* eslint-env browser */
module.exports = typeof self == 'object' ? self.FormData : window.FormData;
/***/ }),

[Note] At the beginning, I thought it was 微信小程序a compatibility or uniappcompatibility problem, but I went to search inaxios , and found that someone had the same problem. GitHub issueAh,,,,, it seems that these three have their own problems.

IssueAddress: node Cannot read property 'FormData' of undefined #5201

insert image description here

After many times of tossing and verification, I don’t recognize anyone who cares, and I have a high probability of solving the case: it is on the WeChat applet, which leads to a problem axiosin the judgment, and there is no such error in the H5 browser (it seems that these two The pot, it’s really not uniappmy pot, haha).

2. Solutions

2.1 Reduce the version of axios

axios0.xThe highest version of is 0.27.2version, so just do the following.

# 移除已有的 axios
 npm uninstall axios
 
# 安装指定版本的 axios
 npm install [email protected]

2.2 Improve to the specified version of axios (recommended)

1.2.1The version is used in the project ( it will report an error ). As of March 18, 2023 , the latest version is 1.3.4, so 1.3.4the version . The result is usable and no error is reported.

From this, it can be concluded that it is estimated that there is also a problem axioswith itself , but it is not known which version is repaired, and there is no in-depth investigation.

# 移除已有的 axios
 npm uninstall axios

# 安装指定版本的 axios
 npm install [email protected]

[Note] Guys, if other versions can also be used, please let me know! I wish you a lot of hair! ! !

insert image description here

Personal blog: Roc's Blog

Guess you like

Origin blog.csdn.net/peng2hui1314/article/details/129637445