Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
axios problem with sending all requests in react

I have an array of items in the local storage and i want to send them all at once to my API problem is when I send them all the API tells me it got all the requests but this loop doesnt remove them from the local storage its like it didn't get a response or something

let local_items = localStorage.getItem("items")
local_items = JSON.parse(local_items)
loadingToast("Syncing items")
for (let i = 0; i < local_items.length; i++) {
  axios.post('/items', {
    item: local_items[i],
    shop: '1',
    employee: '1'
  }, config).then((response) => {
    if (response.status === 200) {
      local_items.splice(i, 1)
      //it counts down 2 or 3 times then gets stuck on a number
      alert("Item synced" + local_items.length)
      localStorage.setItem("items", JSON.stringify(local_items))
    } else {
      dismissToast()
      errorToast("Error syncing items")
      localStorage.setItem("items", JSON.stringify(local_items))
      return
    }
  }).catch((error) => {
    dismissToast()
    errorToast("Error syncing items")
    localStorage.setItem("items", JSON.stringify(local_items))
    return
  })
}
}

the alert shows up for all items but the countdown gets stuck.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

local_items.splice(i, 1) is not a good way to remove the elements, it will only work for the first half elements and not work for the rest.

Try local_items.shift() instead.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda