【发布时间】:2020-05-25 00:28:55
【问题描述】:
我正在尝试从多个不同的来源获取数据。
URL(例如 'this.country["A"] + this.props.productLink')是每个国家的 json 数据。
所以我期望的是每个国家都能得到相应的回报。
但是,当我打印结果时,它会丢失一些国家/地区的值,而是打印其他国家/地区的值,例如 [COUNTRY3,COUNTRY3,COUNTRY4,COUNTRY5,COUNTRY6,COUNTRY7,COUNTRY8,COUNTRY9,COUNTRY3,COUNTRY3]。
结果数组的长度为 10,这与预期的一样。我使用 promises.all 来保存订单。我想知道为什么会这样。
在componentDidMount()下:
fetchData()
{
Promise.all([axios.get(this.country["A"] + this.props.productLink),
axios.get(this.country["B"] + this.props.productLink),
axios.get(this.country["C"] + this.props.productLink),
axios.get(this.country["D"] + this.props.productLink),
axios.get(this.country["E"] + this.props.productLink),
axios.get(this.country["F"] + this.props.productLink),
axios.get(this.country["G"] + this.props.productLink),
axios.get(this.country["H"] + this.props.productLink),
axios.get(this.country["I"] + this.props.productLink),
axios.get(this.country["J"] + this.props.productLink)])
.then(([res1, res2, res3, res4, res5, res6, res7, res8, res9, res10]) => {
return Promise.all([res1.data, res2.data, res3.data, res4.data, res5.data, res6.data, res7.data, res8.data, res9.data, res10.data])
})
.then(([res1, res2, res3, res4, res5, res6, res7, res8, res9, res10]) => {
const rawLoaded = [res1[this.priceSelector],res2[this.priceSelector],res3[this.priceSelector],res4[this.priceSelector],res5[this.priceSelector],
res6[this.priceSelector],res7[this.priceSelector],res8[this.priceSelector],res9[this.priceSelector],res10[this.priceSelector]]
console.log(rawLoaded)
if (rawLoaded.length == 10)
this.setState({isloading:true, rawPrice:rawLoaded})
})
.catch((error) =>{
console.error(error)
})
}
【问题讨论】:
标签: react-native components fetch