[英]axios problem with sending all requests in react
我在本地存储中有一组项目,我想一次将它们全部发送到我的 API 问题是当我将它们全部发送时 API 告诉我它收到了所有请求,但是这个循环不会将它们从本地存储中删除它没有得到回应或什么
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 }) } }
警报显示所有项目,但倒计时卡住了。
local_items.splice(i, 1)
不是删除元素的好方法,它仅适用于前半部分元素,不适用于 rest。
尝试local_items.shift()
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.