2017-06-01 61 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

相關問題