2017-08-30 10 views
0

ReactとReduxを使用して、コンポーネントの状態を更新するために私のすべての処理が完了するまで待つ必要があります。 Axiosドキュメントを読むAxios.allがすべてのアクションを収集し、それらのすべてが解決されるまで待っています(基本的には、APIをn回呼び出してn個のアイテムに関する情報を取得しています)。 axios.allの.then関数内のconsole.logは何も返しませんが、アクションは正しくディスパッチされます。Axios.all in componentWillReceivePropsが呼び出されていない

if (this.props.evaluation && this.props.evaluation.topics) { 
    var promises = [] 

    var array = this.props.evaluation.topics; 
    var selectedArray = [] 
    for (var i = 0; i < array.length; i++) { 
    promises.push(this.props.fetchTopic(array[i])) 
    } 

    axios.all(promises).then(function(results) { 
    results.forEach(function(response) { 
     console.log('///////////////////////'); 
     console.log(response.value); 
     console.log('///////////////////////'); 
    }) 
    }); 
} 

EDIT:ここに私のfetchTopicコード

は私のコンポーネントである:私のactions.jsで

const mapDispatchToProps = (dispatch) => ({ 
    fetchTopic: (id) => dispatch(fetchTopic(id)), 
}) 

export const fetchTopic = (id) => ({ 
    type: "FETCH_TOPIC", 
    payload: axios.get(process.env.SERVICE_URL + '/topic/' + id, { headers: { 'Authorization': 'JWT ' + sessionStorage.jwt }}) 
}) 
+0

を実行するには、 'fetchTopic'コードを追加します。それが約束を返すことを確認してください。 –

+0

アクション作成者はプレーンなjavascriptオブジェクトを返さなければなりません。 –

答えて

0

変更この

この

export const fetchTopic = (id, cb) => { 
    axios.get(process.env.SERVICE_URL + '/topic/' + id, { headers: { 'Authorization': 'JWT ' + sessionStorage.jwt }}) 
    .then((response) => { 
     if (cb) cb({ type: "FETCH_TOPIC", payload:response }) 
    }); 
    } 

から

そして

for (var i = 0; i < array.length; i++) { 
    this.props.fetchTopic(array[i], (result) => { 
    // get the result here 
    }) 
} 
関連する問題