【发布时间】:2021-06-11 06:06:00
【问题描述】:
如果已存在,我想检查并更新 indexeddb 存储中的项目数量。这就是我目前使用 Dexie 将商品添加到商店的方式
async function addNfetch (itemdata) {
return new Promise((resolve, reject) => {
db.table('cartitems')
.add(itemdata)
.then(() =>{
db.table("cartitems").toArray().then((itemArry) => {
console.log("item array ", itemArry)
resolve(itemArry)
})
})
.catch(error => {
console.log(error);
});
})
}
上述函数只在对象键已经存在的情况下添加并抛出错误
DexieError {_e:错误 在 getErrorWithStack (http://localhost:3000/static/js/0.chunk.js:14551:10) 在新的 Dex...,名称:“ConstraintError”,消息:“对象存储中已存在密钥。↵ ConstraintError:对象存储中已存在密钥。”,内部:DOMException:对象存储中已存在密钥。,_promise: DexiePromise,
要检查密钥是否存在,我只能考虑使用 error.message。
if (error.message == "Key already exists in the object store"){
// then object exist
}
有没有更好的方法来检查存储密钥是否已经存在,以便我可以更新而不是覆盖它。
【问题讨论】:
标签: javascript indexeddb dexie