[英]Update shopping cart object inside Meteor user profile
我正在嘗試從購物車中刪除,並更改用戶配置文件中的數量信息:這是結構:
{
"_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 。 並且還可以從購物車中刪除整個屬性。
要更新購物車中的屬性值,請在以下查詢中使用$set運算符:
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}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.