Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda