Javascript 更新Meteor用户配置文件中的购物车对象
我正在尝试从购物车中删除,并更改用户配置文件中的数量信息:结构如下:Javascript 更新Meteor用户配置文件中的购物车对象,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我正在尝试从购物车中删除,并更改用户配置文件中的数量信息:结构如下: { "_id": "3YmDfDWMAaoeQHPAJ", "profile": { "name": "Armin", "wish": [ "FMXRAn3TEXCn6skSX", "avRwogpGvJsKLLKfK", "PJbRh68pwJrtnKe5c", "mt2yqLecy
{
"_id": "3YmDfDWMAaoeQHPAJ",
"profile": {
"name": "Armin",
"wish": [
"FMXRAn3TEXCn6skSX",
"avRwogpGvJsKLLKfK",
"PJbRh68pwJrtnKe5c",
"mt2yqLecyTA2Ejd4n"
],
"cart": {
"mt2yqLecyTA2Ejd4n": "1",
"xqwfyqasfcyTA2ajd": "3",
"xL438DBQrNJTJbPmH": "5"
}
},
"username": "armin"
}
我尝试了以下操作,但它将删除整个购物车
let item = $(event.target).data('id');
Meteor.users.update(Meteor.userId(),{$unset: {"profile.cart": item}});
我的目标是能够更新值1,3,5。还可以从购物车中删除整个属性。要更新购物车中属性的值,请在以下查询中使用类似运算符:
Meteor.users.update(
Meteor.userId(),
{$set: {"profile.cart.xL438DBQrNJTJbPmH": "10"}}
);
要删除购物车中的属性,请执行以下操作:
Meteor.users.update(
Meteor.userId(),
{$unset: {"profile.cart.xL438DBQrNJTJbPmH": ""}}
);
$unset表达式中的指定值(即“”)不会影响操作
在必须动态构造属性名称的情况下,可以使用其他变量:
var condition = {};
condition["profile.cart." + dynamicPart] = "";
Meteor.users.update(
Meteor.userId(),
{$unset: condition}
);
您可以使用
delete[PropertyName]
删除javascript中的属性;使用delete
关键字我尝试delete Meteor.user().profile.cart[item]
但我想它在Meteor中不起作用。当我使用确切的“profile.cart.xl438dbqrnjjjbpmh”
时,效果很好。问题是我需要像这样附加idnewItem=“profile.cart.”+receivedId
然后使用它“{$unset:{newItem:“}}}在本例中,我得到一个访问被拒绝的控制台日志。update:我认为它是通过在receivedId周围添加[]来工作的。