【发布时间】:2020-12-19 11:24:07
【问题描述】:
class myFetchData extends Component {
constructor(props){
super(props)
this.state = {
data : [],
Issuer_News : []
}
}
componentDidMount(){
this.setState({loading : true})
axios.post('https://bqskvn9laah.execute-api.ap-south-1.amazonaws.com/Dev/datalist',{
DATA: this.state.data
}).then((response) => {
this.setState({data : res, number_of_data : response.data[1].total_data_issued_by_the_company,loading : false})})
.catch(err => console.log(err))
fetch(`https://gnews.io/api/v3/search?q=${this.state.data.Issuer_Name}&max=5&token=52498bb02769e98d131156c2648628ca`)
.then( response => response.json())
.then(data => this.setState({Issuer_News : data.articles}))
}
我想要来自 API 1 的数据并将数据从 API 1 传递到 API 2 但 this.state.data 返回 undefined。
【问题讨论】:
-
API 调用和 setState 一样是异步的。它不会等待调用完成并进行第二次 API 调用。您应该在 .then 中进行第二次调用
标签: javascript reactjs react-native api fetch