简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM