gpt4 book ai didi

javascript - 如何删除 IndexedDB 中的所有记录

转载 作者:行者123 更新时间:2023-12-02 22:41:16 27 4
gpt4 key购买 nike

我有一个 IndexedDB,用于存储所有购物车订单。结帐时,购物车需要保持干净。我试图循环遍历每个订单并删除,但不知何故,只有第一个订单被删除。这是我的代码:

const clear_cart = () => {
let objectCart = db.transaction('cart').objectStore('cart');

objectCart.openCursor().onsuccess = function(e){
let cursor = e.target.result;
if(cursor){
let cartId = cursor.value.id;

let transaction = db.transaction(['cart'], 'readwrite');
let objectToDelete = transaction.objectStore('cart');
let request = objectCart.objectToDelete(cartId);

transaction.oncomplete = () => {
console.log(`cart ${cartId} is deleted!`);
}

cursor.continue();
}
}
}

最佳答案

我对 dmigo 的建议做了一些调整,这解决了我的问题。这是代码

const clear_cart = () => {
let objectCart = db.transaction('cart').objectStore('cart');

let transaction = db.transaction(['cart'], 'readwrite');
let objectToDelete = transaction.objectStore('cart');

objectToDelete.clear().onsuccess = function(e){
console.log(`the cart is clear!`);
}
}

关于javascript - 如何删除 IndexedDB 中的所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com