data1 是一個包含來自以下回圈的 5 個鏈接的陣列。
let data1 = [];
console.log(data1);
for (let Items of ItemsArray) {
const row = {
PricingSummary: Items.pricingOptionsSummaryUrl,
}
data1.push(row);
};
我想使用這個陣列來執行 5 次 GET 請求Axios.all。
得到回應后,我想 setState 來映射我想要的資料。
const callprice = () => { //assign a variable for a call function
Axios.all(data1.map(l => Axios.get(l)))
.then(Axios.spread(function(...res) {
// setStats(....)
console.log(res); // Getting error message : xhr.js:210 GET http://localhost:3000/[object Object] 404 (Not Found)
}));
};
我收到本地主機 404(未找到)錯誤。相信來自 的請求陣列鏈接不正確data1,但不確定如何使它們正確。
控制臺示例data1:
0: {PricingSummary: 'http://snapshot.dellsvc/snapshots/MXL5hLvzBkajQ-fqGTo9oA'}
1: {PricingSummary: 'http://snapshot.dellsvc/snapshots/3gmDYxoCg0m9YgWB3aLLpA'}
2: {PricingSummary: 'http://snapshot.dellsvc/snapshots/dEpCHAi3IUe1sTIqEo9Idw'}
3: {PricingSummary: 'http://snapshot.dellsvc/snapshots/SAIS_lcIxU202-Mnm5KLIQ'}
4: {PricingSummary: 'http://snapshot.dellsvc/snapshots/H_9txy3Ejkm-zoe49Hbkzg'}
5: {PricingSummary: undefined}
uj5u.com熱心網友回復:
首先,axios.all()和axios.spread()都已棄用。您應該改用Promise.all()或Promise.allSettled()。
其次,似乎您的 URL 中至少有一個undefined在請求中肯定不起作用。您可能希望過濾掉這些問題記錄。
第三,您將具有PricingSummary屬性的物件推送到data1. 這些不能用于 inaxios.get()因為它需要一個 URL string,而不是一個物件。這就是你[object Object]的來源。
const callprice = async () => {
const responses = await Promise.all(
ItemsArray
.filter(item => item.pricingOptionsSummaryUrl) // remove falsy values
.map(async (item) => (await axios.get(item.pricingOptionsSummaryUrl)).data)
)
// now `responses` is an array of the response data
}
uj5u.com熱心網友回復:
你應該使用 Promise.allSettled 代替
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/369741.html
標籤:javascript 数组 反应 公理
