data1
es una matriz con 5 enlaces del siguiente bucle.
let data1 = []; console.log(data1); for (let Items of ItemsArray) { const row = { PricingSummary: Items.pricingOptionsSummaryUrl, } data1.push(row); };
Quiero usar esta matriz para realizar solicitudes GET 5 veces usando Axios.all
.
Después de recibir la respuesta, quiero establecer el Estado para mapear con los datos que quiero.
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%20Object] 404 (Not Found) })); };
Recibo los errores localhost 404 (No encontrado). Cree que los enlaces de la matriz de solicitud son incorrectos desde data1
, pero no está seguro de cómo corregirlos.
Ejemplo de la consola para el 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}
Primero, axios.all()
y axios.spread()
están obsoletos . Debería usar Promise.all() o Promise.allSettled() en su lugar.
En segundo lugar, parece que al menos una de sus URL no está undefined
, lo que definitivamente no funcionará en una solicitud. Es posible que desee filtrar estos registros de problemas.
En tercer lugar, está insertando objetos con una propiedad PricingSummary
en data1
. Estos no se pueden usar en axios.get()
ya que espera una cadena de URL, no un objeto. Aquí es de donde proviene su [object%20Object]
.
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 }
Deberías usar Promise.allSettled en su lugar