如何使用 dexie 更新 indexeddb 存储项目
how to update indexeddb store items with dexie
我有索引数据库 table
"00014381104394"
{id: "00014381104394", name: "A House Divided: Season 1 (DVD)", productimg: "http://i5.walmartimages.com/asr/99cfec5c-634e-4e26…4465.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
1 "00016500590958"
{id: "00016500590958", name: "One A Day Men's 50+ Mini Gels, Multivitamins for Men, 80 Ct", productimg: "http://i5.walmartimages.com/asr/7d44d419-bd6f-4808…b7ea.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
2 "00022141041599"
{id: "00022141041599", name: "Mepps Dressed Aglia Inline Spinner, Silver & Gray, 1/4 oz", productimg: "http://i5.walmartimages.com/asr/a0ce2579-300a-4536…0d63.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
3 "00038257767124"
{id: "00038257767124", name: "Hanes Men' Max Cushion Comfort Top B&T Crew Socks 6 Pack", productimg: "http://i5.walmartimages.com/asr/f439dc53-c905-45bb…3436.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
4 "00041333002132"
{id: "00041333002132", name: "Duracell Coppertop AAA Battery, Long Lasting Triple A Batteries, 24 Pack", productimg: "http://i5.walmartimages.com/asr/09b2aa97-6655-4422…2259d.png?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
我需要更新每件商品的数量,所以我尝试了下面的代码
db.cartitems.get(newItemId)
.then((result) => {
if (result){
db.table("cartitems").toArray().then((cartitemArry) => {
var cartitemArry1 = []
cartitemArry.forEach(product => {
if (product.id == newItemId){
// increase item quantity
var qty = parseInt(product["quantity"])+1
product["quantity"] = qty
console.log(product.id," was found in cart. Qty increased from ",qty, "to ", product["quantity"])
cartitemArry1.push(product)
console.log("cart array push at ",newItemId,"\n", cartitemArry1)
}else{
cartitemArry1.push(product)
}
console.log("cart array", cartitemArry1)
});
db.table('cartitems').bulkPut(cartitemArry1)
.then(() =>{
console.log('finished updating cart')
}).catch(error => {
console.log(error);
});
resolve(cartitemArry1)
})
以上代码获取 table 中的所有项目,但不保存更新后的数量。我只想能够增加或减少指定 itemid 的数量。
我不知道如何直接更新特定 itemid 的 属性。因此,我获取所需的商品,然后获取商店中的所有商品,并创建一个包含所有商品的新数组,以包含所需商品的更新数量,然后使用新数组对同一商店执行 bulkPut。 –
实现此目标的最佳方法是什么?
如果您只想增加 cartitems 中给定 newItemId 的“数量”属性,请执行:
db.transaction('rw', db.cartitems, () => {
return db.cartitems.get(newItemId).then(item => {
++item.quantity;
return db.cartitems.put(item);
});
});
你也可以这样做(相当于上面的片段):
db.cartitems.where({id: newItemId}).modify(item => ++item.quantity);
查看文档:
我有索引数据库 table
"00014381104394"
{id: "00014381104394", name: "A House Divided: Season 1 (DVD)", productimg: "http://i5.walmartimages.com/asr/99cfec5c-634e-4e26…4465.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
1 "00016500590958"
{id: "00016500590958", name: "One A Day Men's 50+ Mini Gels, Multivitamins for Men, 80 Ct", productimg: "http://i5.walmartimages.com/asr/7d44d419-bd6f-4808…b7ea.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
2 "00022141041599"
{id: "00022141041599", name: "Mepps Dressed Aglia Inline Spinner, Silver & Gray, 1/4 oz", productimg: "http://i5.walmartimages.com/asr/a0ce2579-300a-4536…0d63.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
3 "00038257767124"
{id: "00038257767124", name: "Hanes Men' Max Cushion Comfort Top B&T Crew Socks 6 Pack", productimg: "http://i5.walmartimages.com/asr/f439dc53-c905-45bb…3436.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
4 "00041333002132"
{id: "00041333002132", name: "Duracell Coppertop AAA Battery, Long Lasting Triple A Batteries, 24 Pack", productimg: "http://i5.walmartimages.com/asr/09b2aa97-6655-4422…2259d.png?odnHeight=180&odnWidth=180&odnBg=ffffff", unitofissue: "each", quantity: 1, …}
我需要更新每件商品的数量,所以我尝试了下面的代码
db.cartitems.get(newItemId)
.then((result) => {
if (result){
db.table("cartitems").toArray().then((cartitemArry) => {
var cartitemArry1 = []
cartitemArry.forEach(product => {
if (product.id == newItemId){
// increase item quantity
var qty = parseInt(product["quantity"])+1
product["quantity"] = qty
console.log(product.id," was found in cart. Qty increased from ",qty, "to ", product["quantity"])
cartitemArry1.push(product)
console.log("cart array push at ",newItemId,"\n", cartitemArry1)
}else{
cartitemArry1.push(product)
}
console.log("cart array", cartitemArry1)
});
db.table('cartitems').bulkPut(cartitemArry1)
.then(() =>{
console.log('finished updating cart')
}).catch(error => {
console.log(error);
});
resolve(cartitemArry1)
})
以上代码获取 table 中的所有项目,但不保存更新后的数量。我只想能够增加或减少指定 itemid 的数量。 我不知道如何直接更新特定 itemid 的 属性。因此,我获取所需的商品,然后获取商店中的所有商品,并创建一个包含所有商品的新数组,以包含所需商品的更新数量,然后使用新数组对同一商店执行 bulkPut。 – 实现此目标的最佳方法是什么?
如果您只想增加 cartitems 中给定 newItemId 的“数量”属性,请执行:
db.transaction('rw', db.cartitems, () => {
return db.cartitems.get(newItemId).then(item => {
++item.quantity;
return db.cartitems.put(item);
});
});
你也可以这样做(相当于上面的片段):
db.cartitems.where({id: newItemId}).modify(item => ++item.quantity);
查看文档: