【发布时间】:2017-11-02 10:07:15
【问题描述】:
首先,我知道我必须返回承诺以避免此警告。我也尝试按照here 的建议返回null,但它不起作用。这是我的一段代码:
import React, { Component } from 'react';
import request from 'superagent-bluebird-promise';
function loadCountry(url, countryId) {
return request.get(`${url}/countries/${countryId}`).set('Accept', 'application/json').
then(response => response.body.name || response.body.id).catch(errors => null);
}
class AddressCountry extends Component {
...
componentDidMount() {
loadCountry(this.props.actionUrl, this.props.countryId).then(country => {
this.setState({ country: country });
});
}
}
我尝试在this.setState({ country: country }); 行之后返回null,但它不起作用。
这是我得到的错误:
Warning: a promise was created in a handler at eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1201:1), <anonymous>:65:16 but was not returned from it
at new Promise (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:2064:1), <anonymous>:2715:26)
at <anonymous>
From previous event:
at Request.promise (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:728:1), <anonymous>:72:10)
at loadCountry (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4762:1), <anonymous>:25:157)
at AddressCountry.componentDidMount (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4762:1), <anonymous>:51:7)
at measureLifeCyclePerf (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:3959:1), <anonymous>:77:12)
at Object.batchedUpdates (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4078:1), <anonymous>:62:26)
at enqueueUpdate (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1103:1), <anonymous>:26:16)
at SupplierAddressEditor.ReactComponent.setState (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:1201:1), <anonymous>:65:16)
From previous event:
at measureLifeCyclePerf (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:3959:1), <anonymous>:77:12)
at Object.batchedUpdates (eval at <anonymous> (http://localhost:8080/supplier/static/bundle.js:4078:1), <anonymous>:62:26)
PS:不是this question 也不是this 的重复项
【问题讨论】:
标签: javascript reactjs bluebird superagent