1

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. 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):

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.

0

1 Answer 1

1

Use the bracket notation for dynamic object keys:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.