我不断收到此错误:未捕获(承诺)TypeError:_this2.doSomething不是函数

时间:2018-09-21 21:30:42

标签: javascript reactjs

我已经为此苦苦挣扎了两个小时,但都没有成功。 我试图从“ Promise”调用我的react组件中定义的“ doSomething”函数,但我没有上下文。

有人可以指出我离开这里的路吗?

这里是我要做什么的一些解释。

class Example extends React.Component{
constructor(){
    super();
    this.state = { /**/ }
    this.doSomething.bind(this);
}

doSomething(data){
    /*do something with data*/
}

callApi(){
    const headers = new Headers();
    const options = {
        method: 'POST',
        headers: headers,
        mode: 'cors',
        cache: 'default',
    };
    fetch('http://localhost/api?format=json', options).then(result => {
        result.json().then(data => {
                this.doSomething(data);
            }
        );
    });
}

}

0 个答案:

没有答案