React native React Native Multiple Fetch返回错误的值

React native React Native Multiple Fetch返回错误的值,react-native,components,fetch,React Native,Components,Fetch,我正在尝试从多个不同的来源获取数据 URL(例如'this.country[“A”]+this.props.productLink')是每个国家的json数据 所以我希望每个国家都能得到相应的回报。 但是,当我打印结果时,它会忽略一些国家的值,而是打印其他国家的值,如[COUNTRY3,COUNTRY3,COUNTRY4,COUNTRY5,COUNTRY6,COUNTRY7,COUNTRY8,COUNTRY9,COUNTRY3,COUNTRY3]。 结果数组的长度为10,这与预期的一样。我用承诺


我正在尝试从多个不同的来源获取数据
URL(例如'this.country[“A”]+this.props.productLink')是每个国家的json数据
所以我希望每个国家都能得到相应的回报。 但是,当我打印结果时,它会忽略一些国家的值,而是打印其他国家的值,如[COUNTRY3,COUNTRY3,COUNTRY4,COUNTRY5,COUNTRY6,COUNTRY7,COUNTRY8,COUNTRY9,COUNTRY3,COUNTRY3]。 结果数组的长度为10,这与预期的一样。我用承诺来维持秩序。我想知道为什么会这样

在componentDidMount()下:


我认为这可能是因为您处理数据的方式。首先,您不需要在两个
中都手动创建新数组。然后()
回调,更好的做法是在现有数组上调用
.map()
。您也不需要在收到来自
axios
的响应后调用
Promise.all()
,因为您传递的内容都不是
Promise
。试一试下面的实现

函数fetchData(){
const{productLink}=this.props;
我保证([
axios.get(this.country[“A”]+productLink),
axios.get(this.country[“B”]+productLink),
axios.get(this.country[“C”]+productLink),
axios.get(this.country[“D”]+productLink),
axios.get(this.country[“E”]+productLink),
axios.get(this.country[“F”]+productLink),
axios.get(this.country[“G”]+productLink),
axios.get(this.country[“H”]+productLink),
axios.get(this.country[“I”]+productLink),
axios.get(this.country[“J”]+productLink)
])
。然后(结果=>{
const resultData=results.map(res=>res.data);
const rawload=resultData.map(data=>data[this.priceSelector]);
如果(rawload.length!=10){
console.warn(“意外输出”);
返回;
}
这个.setState({
孤岛加载:是的,
原价:原价
});
})
.catch(错误=>{
warn(`提取数据时出错-${error}`);
});
}

非常感谢。我尝试了你的代码,但打印时仍然得到错误的结果。我从包含“p/ajax/file.json”的url获取。它与ajax的行为有什么关系吗?
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)
    })
}