2016-05-20 72 views
0

在我的req.session.cart我有数量的对象。我想在每次将特定项作为查询参数传递时更新数量字段。我试过,但我只能追加另一个数量字段而不是更新它。Node.js更新会话字段

[ 
    { 
    "_id": "573e8cb8d41066b112908598", 
    "quantity": 1 
    }, 
    { 
    "_id": "573e8c6ad41066b112908597", 

    "quantity": 1 
    } 
] 

这就是我试图

function processEdit(req, res, next) { 
    var quantity = req.body.quantity; 
    req.session.cart.forEach(function(data) { 
     if (data._id == req.params._id) { 
      arrayTrue = false; 
     } 
    }); 
    if (arrayTrue) { 
     req.session.cart.quantity = quantity; 
     console.log(req.session.cart); 
    } 
    res.send(req.session.cart); 
+1

哪里是你更新的代码? –

+0

@vp_arth我已经更新了我试过的 – learner

+0

'req.session.cart [index] .quantity' - 你的购物车是数组 –

回答

0

您的购物车是一个数组。

req.session.cart[index].quantity++如果您有索引。

如果你有一个id:

req.session.cart.forEach(item => { 
    if (item._id == id) { 
    item.quantity++; 
    } 
}); 
+0

谢谢它的工作。 – learner