I have Indexed db 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, …}
i need to update the each item's quantity so I tried the code below
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)
})
The above code fetches all items in table but does not save the updated quantity. I just want to be able to increase or decrease the quantity of a specified itemid. I didn't know how to directly update the property of a specific itemid. So I fetch a desired item and then all items in the store and create a new array with all items in store to include the updated quantity of the desired item and then do a bulkPut to the same store with the new array. – What is the best way to achieve this?
If you just want to increment the "quantity" property of given newItemId in cartitems, do:
db.transaction('rw', db.cartitems, () => {
return db.cartitems.get(newItemId).then(item => {
++item.quantity;
return db.cartitems.put(item);
});
});
You could also do this (which is equivalent to the snipped above):
db.cartitems.where({id: newItemId}).modify(item => ++item.quantity);
See docs: