I want to update fields in nested objects in my Cloud Firestore by using dot notation. But those fields referenced by the dot notation are variables.
How can I do that?
Documentation// To update age and favorite color:
db.collection("users").doc("frank").update({
"age": 13,
"favorites.color": "Red"
})
But what if I need to make favorites.color
a variable?
const attributes = [ 'jobs', 'schools', 'favorites', ];
const values = [ 'food', 'song', 'color', ];
const variableData = [attributes[2]].[values[2]]; // 'favorites.color'
Can I do:
db.collection("users").doc("frank").update({
"age": 13,
"[attributes[2]].[values[2]]": "Red"
})
How, exactly, would this work?
Edit:
Failed attempts
I tried:
db.collection("users").doc("frank").update({
"age": 13,
`${[attributes[2]]}.${[values[2]]}`: "Red"
})
and got the following error:
Parsing error: Unexpected token
Same error also occurs when trying
`"${[attributes[2]]}.${[values[2]]}"`: "Red"
(Thinking that Firebase needs the quotation marks to parse the syntax.)