Javascript 如何将数据从一个api存储区提取到状态,然后将此数据传递到另一个api 类myFetchData扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 数据:[], 发行人新闻:[] } } componentDidMount(){ this.setState({loading:true}) 轴心柱https://bqskvn9laah.execute-api.ap-south-1.amazonaws.com/Dev/datalist',{ 数据:this.state.DATA })。然后((响应)=>{ this.setState({data:res,number\u of\u data:response.data[1]。由\u公司发布的\u数据总量,加载:false}) .catch(err=>console.log(err)) 取回(`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,但是Javascript 如何将数据从一个api存储区提取到状态,然后将此数据传递到另一个api 类myFetchData扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 数据:[], 发行人新闻:[] } } componentDidMount(){ this.setState({loading:true}) 轴心柱https://bqskvn9laah.execute-api.ap-south-1.amazonaws.com/Dev/datalist',{ 数据:this.state.DATA })。然后((响应)=>{ this.setState({data:res,number\u of\u data:response.data[1]。由\u公司发布的\u数据总量,加载:false}) .catch(err=>console.log(err)) 取回(`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})) },javascript,reactjs,react-native,api,fetch,Javascript,Reactjs,React Native,Api,Fetch,我希望从API 1获得数据,并将数据从API 1传递到API 2,但是this.state.data返回未定义的在ReactJS中,setState和fetch函数都是异步的,所以这就是为什么没有定义的原因。因为API 2可以在返回API 1的成功承诺之前运行。我更喜欢调用的fetch函数成功承诺中的API 2。就像这样 axios.post('https://bqskvn9laah.execute-api.ap-south-1.amazonaws.com/Dev/datalist',{
this.state.data
返回未定义的在ReactJS中,setState和fetch函数都是异步的,所以这就是为什么没有定义的原因。因为API 2可以在返回API 1的成功承诺之前运行。我更喜欢调用的fetch函数成功承诺中的API 2。就像这样
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})
fetch(`https://gnews.io/api/v3/search?q=${response.data.Issuer_Name}&max=5&token=52498bb02769e98d131156c2648628ca`)
.then( response => response.json())
.then(data => this.setState({Issuer_News : data.articles}))
}).catch(err => console.log(err))
我建议您使用一些状态管理器库,如redux、redux saga,以便更轻松地控制api调用流在ReactJS中,setState和fetch函数都是异步的,因此这就是您未定义的原因。因为api 2可以在返回api 1的成功承诺之前运行。我更喜欢调用api 2的fetch函数在成功的承诺中,就像这样
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})
fetch(`https://gnews.io/api/v3/search?q=${response.data.Issuer_Name}&max=5&token=52498bb02769e98d131156c2648628ca`)
.then( response => response.json())
.then(data => this.setState({Issuer_News : data.articles}))
}).catch(err => console.log(err))
我建议您使用一些状态管理器库,如redux、redux saga,以便更轻松地控制api调用流在这种情况下,如果您希望链接api调用,则需要在then()的setState方法中调用第二个api接收data.method的第一个api块有一个变量,在该变量中,您可以将函数作为参数传递,它确保只有在调用setState时才运行该函数
如果您使用的是this.state.data
,则仅在then()块中进行调用是不够的。您需要将fetch调用放入setState的回调函数中。如果不想使用回调函数变量,则只需在then()方法中直接使用response变量
假设axios.post
是第一个API,而fetch
是第二个API
componentDidMount(){
this.setState({loading : true})
axios.post('<yourUrl>', {
...
}).then((response) => {
this.setState({<setYourState>}, () => {
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}))
}
})
})
.catch(err => console.log(err))
componentDidMount(){
this.setState({loading:true})
轴心柱(“”{
...
})。然后((响应)=>{
this.setState({},()=>{
取回(`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}))
}
})
})
.catch(err=>console.log(err))
在这种情况下,如果要链接api调用,则需要在接收数据的第一个api的setState方法then()块中调用第二个api。该方法有一个变量,可以将函数作为参数传递,并确保仅在调用setState时才运行该函数
如果您使用的是this.state.data
,则仅在then()块中进行调用是不够的。您需要将fetch调用放入setState的回调函数中。如果不想使用回调函数变量,则只需在then()方法中直接使用response变量
假设axios.post
是第一个API,而fetch
是第二个API
componentDidMount(){
this.setState({loading : true})
axios.post('<yourUrl>', {
...
}).then((response) => {
this.setState({<setYourState>}, () => {
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}))
}
})
})
.catch(err => console.log(err))
componentDidMount(){
this.setState({loading:true})
轴心柱(“”{
...
})。然后((响应)=>{
this.setState({},()=>{
取回(`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}))
}
})
})
.catch(err=>console.log(err))
API调用是异步的,也是设置状态的。它不会等待调用完成并进行第二次API调用。您应该在中进行第二次调用。然后blockAPI调用是异步的,也是设置状态的。它不会等待调用完成并进行第二次API调用。您应该在中进行第二次调用。然后block