简体   繁体   English

firebase firestore 在运行时更新变量嵌套数据

[英]firebase firestore update variable nested data at runtime

How can I update a nested value that is undetermined until runtime?如何更新直到运行时才确定的嵌套值?

For example, I wish to update the value at the key which is a user's id.例如,我希望更新作为用户 ID 的键的值。 I don't know which user to update until runtime.直到运行时我才知道要更新哪个用户。

The docs give this example code ( https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects ):文档给出了这个示例代码( https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects ):

db.collection("users").doc("frank").update({
    "age": 13,
    "favorites.color": "Red"
})

I have modified it using template literals to suit my purposes:我已经使用模板文字对其进行了修改以适合我的目的:

db.collection("chatGroups").doc(route.params.data.id).update({
    `markedDates.${auth.currentUser.uid}`: markedDates
});

but it throws syntax errors.但它会引发语法错误。

Use the bracket notation for dynamic object keys:对动态 object 键使用括号表示法

db.collection("chatGroups").doc(route.params.data.id).update({
    [`markedDates.${auth.currentUser.uid}`]: markedDates
});

The specs for it are here: https://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer它的规格在这里: https://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer

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

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