2017-06-01 43 views
0

首先,我知道必须返回承诺以避免此警告。我也试过按照建议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

回答

0

您需要在componentDidMount

componentDidMount() { 
     return loadCountry(this.props.actionUrl, this.props.countryId).then(country => this.setState({ country })); 
     } 
+0

返回从loadCountry()的承诺重复我不要这样但仍显示相同的警告:( – Lekeasong