错误:未捕获(承诺)TypeError:_this2.next不是reactJS中的函数

时间:2018-12-03 16:14:09

标签: javascript reactjs ecmascript-6

我收到此错误,请您帮我消除此错误。我的申请中有4个案件。在情况1中,您可以查看我的第2步代码。请帮助我,我真的很困难

case 1:
        // code run for step 2

        const reader = new FileReader();
        const storeUser = JSON.parse(localStorage.getItem('user'));
        reader.onload = function(upload) {
          fetch(`...../api/s3/uploadtoaws`, {
            headers: {
              Accept: 'application/json',
              'Content-Type': 'application/json',
            },
            method: 'POST',
            body: JSON.stringify({
              userId: storeUser._id,
              type: 'employee',
              content: upload.target.result,
              key: 'e.file.name',
              oldKey: '',
            }),
          })
            .then(response => response.json())
            .then(res => {
              if (res.error) {
                console.warn(res);
              } else {
                console.warn(res);
                this.next();
              }
            })
            .done();
        };
        reader.readAsDataURL(values.picture.file.originFileObj);
        break;

1 个答案:

答案 0 :(得分:1)

  

reader.onload =函数(上传){

由于这是一个普通函数,因此this的值由其调用方式决定。我假设FileReader用this等于全局对象(在非严格模式下)或未定义(在严格模式下)调用它。尽管由于我不确定FileReader代码的实现方式,所以它可能具有this等于其他内容,例如文件读取器本身。无论哪种方式,稍后在调用this.next时,this都不等于您认为的那样。

如果将其更改为箭头功能,它将从其定义的位置获取其值this

reader.onload = (upload) => {

请注意,这可能还不够,因为这还取决于您提供的示例周围的代码。您可能需要将其他功能转换为箭头功能,和/或使用function.prototype.bind