2017-04-25 65 views
0

我在react.js應用程序中使用axios。如何使用api中的數據調用其他api反應js

我正在調用一個API來獲取訪問令牌。我在調用componentWillMount時調用其他API。

我需要通過從第一API調用其他API調用生成的訪問令牌。

我該怎麼做?

我的代碼:

componentWillMount() { 
    if(!isLoggedIn()){ 
     this.props.actions.token(); 
    //other api calls 
     this.props.actions.getData(); 
    }  
} 

謝謝

回答

1

你可能將不得不保存訪問令牌在類似 localstorage,然後訪問它時,你正在另一個請求。

或者,如果您只是將它用於一個目的, 您可能想要的是在獲取承諾鏈中的訪問令牌之後再次進行api調用。

fetchToken().then((response) => { 
    return response.accessToken; // Get access token 
}).then((accessToken) => { 
    return someOtherApiCall(accessToken); Make another call with the access token 
}).then((data) => { 
// Required Data 
});