[英]ReactJS: "TypeError: Object(...)(...) is undefined"
在下面的代码中:
handleImageChange = (newFile, crop) => {
var string = RandomString.generate(20);
newImage(string, newFile, this.state.type, 0)
.then((result) => {
if(result) {
this.toggleModal();
}
});
}
saveCropped() {
var values = this.cropper.values();
var crop = this.cropper.crop();
var newFile = convertBase64ToFile(crop);
setTimeout(function() {
this.handleImageChange(newFile, crop);
}.bind(this), 5000);
}
调用的函数是saveCropped()
,然后调用handleImageChange
函数。
这个函数的作用(newImage)是向我的服务器发送一个 Axios 请求,但是当我运行它时,我收到以下错误:“TypeError: Object(...)(...) is undefined”
问题是,即使我收到此错误,代码在后台执行也没有问题,即请求被发送到我的服务器,我在控制台中得到响应。
错误指向第 5 行(newImage(...))
。
newImage 函数之所以有效,是因为这不是唯一调用它的地方,而且该函数只返回一个布尔值; 即便如此,它还是:
function newImage(image_ID, image, imgType, type) {
var url = ...;
const header = {
headers: {
'Content-Type': imgType
}
};
axios.post(url, image, header)
.then(function (response) {
if(response.status === 200) {
if(type !== '1')
sessionStorage.setItem('avatar', image_ID);
console.log("Image uploaded!");
return true;
}
else
return false;
})
.catch(function (error) {
console.log(`Image could not be uploaded due to:\n${error}`);
return false;
});
}
我在这里做错了什么?
您试图在newImage()
的返回值上调用then()
() ......但该函数没有 return 语句。
大概,您想返回通过调用axios.post
返回的承诺:
return axios.post(url, image, header).
etc etc
我知道这个特殊的错误已经解决了。 但对于未来的搜索者来说,这可能值得注意; 我收到了这个错误,原来是因为配置对象连续有两个逗号,如下所示:
{
name: "Something"
},,{
name: "Foobar"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.