【问题标题】:how to update indexeddb store items with dexie如何使用 dexie 更新 indexeddb 存储项目
【发布时间】:2021-06-24 04:46:09
【问题描述】:

我有索引数据库表

    "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)
                     })

以上代码获取表中的所有项目,但不保存更新的数量。我只是希望能够增加或减少指定 itemid 的数量。 我不知道如何直接更新特定 itemid 的属性。因此,我获取所需的商品,然后获取商店中的所有商品,并创建一个包含商店中所有商品的新数组,以包含所需商品的更新数量,然后使用新数组对同一商店执行 bulkPut。 – 实现这一目标的最佳方法是什么?

【问题讨论】:

  • db.cartitems.get(newItemId)返回你想要的,为什么你需要获取所有的项目?
  • @charlietfl 我很确定我做错了。我不知道如何直接更新特定 itemid 的属性。因此,获取商品,然后获取商店中的所有商品,并使用更新后的数量创建一个新数组,然后我是否想获取特定商品,然后使用新数组对同一商店执行 bulkPut。
  • 好的。 result 应该是您要更新的单个项目(假设它存在),而不是 bulkPut 只是 put()。检查文档,我很久没看它们了,但我 95% 确定它只是 put() 用于单个项目

标签: javascript indexeddb dexie


【解决方案1】:

如果您只是想增加 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.get()

Table.put()

Collection.modify()

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2021-02-13
    • 2018-09-25
    • 2019-06-17
    • 2021-10-15
    • 2012-03-21
    • 2018-03-18
    • 2021-05-20
    • 2022-10-08
    相关资源
    最近更新 更多