简体   繁体   English

更新 Meteor 用户配置文件中的购物车对象

[英]Update shopping cart object inside Meteor user profile

I'm trying to delete from shopping cart, and change quantity information in user profile: here is the structure:我正在尝试从购物车中删除,并更改用户配置文件中的数量信息:这是结构:

    {
      "_id": "3YmDfDWMAaoeQHPAJ",
      "profile": {
        "name": "Armin",
        "wish": [
          "FMXRAn3TEXCn6skSX",
          "avRwogpGvJsKLLKfK",
          "PJbRh68pwJrtnKe5c",
          "mt2yqLecyTA2Ejd4n"
        ],
        "cart": {
          "mt2yqLecyTA2Ejd4n": "1",
          "xqwfyqasfcyTA2ajd": "3",
          "xL438DBQrNJTJbPmH": "5"
        }
      },
      "username": "armin"
    }

I tried the following but it will delete the whole cart我尝试了以下但它会删除整个购物车

    let item = $(event.target).data('id');
    Meteor.users.update(Meteor.userId(),{$unset: {"profile.cart": item}});

My goal is to be able to update the value 1,3,5 .我的目标是能够更新值 1,3,5 。 and also be able to delete a whole property from cart.并且还可以从购物车中删除整个属性。

To update an attribute's value in the cart, use $set operator like in the following query:要更新购物车中的属性值,请在以下查询中使用$set运算符:

Meteor.users.update(
    Meteor.userId(),
    {$set: {"profile.cart.xL438DBQrNJTJbPmH": "10"}}
);

To delete an attribute in the cart:要删除购物车中的属性:

Meteor.users.update(
    Meteor.userId(), 
    {$unset: {"profile.cart.xL438DBQrNJTJbPmH": ""}}
);

The specified value in the $unset expression (ie "") does not impact the operation. $unset 表达式中的指定值(即“”)不会影响操作。

In the case when you have to construct the attribute's name dynamically, you can use an additional variable:在必须动态构造属性名称的情况下,可以使用附加变量:

var condition = {}; 
condition["profile.cart." + dynamicPart] = "";
Meteor.users.update(
    Meteor.userId(), 
    {$unset: condition}
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM