簡體   English   中英

axios 在反應中發送所有請求的問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM