How can one update fields in a document's nested object? The documentation indicated dot notation but how do you achieve the update with a variable as the object name?
Structure is like: collection("fruits").doc("as867f56asd")
name: "banana"
abc-variable:
description:"blah"
qty: 1
I want to update document as867f56asd's abc-variable.qty = 2
My JavaScript requires the use of a variable for the object name.
I can't figure out bracket notation for the update. Is .set{merge:true} req'd?
Here is the code I've tried so far (rough paste):
qty = evt.target.value;
//create a string
var obj = firebase.firestore.FieldPath(item).quantity;
//str = item + '.quantity';
//obj[str] = qty;
// var myUpdate = {};
// myUpdate['${item}.quantity'] = qty;
//var obj = [item]["quantity:"] = qty;
console.log("item is " + item);
//var obj = {"'" + item + "'.quantity" : qty};
//obj[item]["quantity"] = qty;
console.log("qty is " + qty);
orderRef.update (
{obj:2}
//{"quantity":qty}
//[item + '.quantity']: qty
//[`favorites.${key}.color`] = true
// ['${item}.quantity'] : qty
//[`hello.${world}`]:
//{[objname]}.value = 'value';
//['favorites.' + key + '.color']: true
//[item]["quantity"] = qty // err:reqd 2 args
//item["quantity"] = qty
//"favorites.color": "Red"
//{"`item`.quantity": qty}
//{"quantity":qty}
)
values[abc-variable+".qty"] = 2, but it's easier to answer if I see what you've done already..update(IDE saysvaluesis not defined. If I wrap it in{}it saysUnexpected token [. If I declarevalues = {}prior to the update, the IDE takes it and it passes linting but browser console throws errorUncaught TypeError: Cannot read property 'quantity' of undefinedDoing:orderRef.update (values[item+".quantity"] = qty)FieldPathor even defining a reference using URLencoding\fruits\abc-123doc\obj-field-varbut I can't figure it out. I bout to give up and just re-write the whole document to update a single field. I know that works...myUpdate['${item}.quantity'] = qty;should probably bemyUpdate[`${item}.quantity`] = qty;. So with backticks around the string, since that's the JavaScript syntax for string interpolation.itemin the code you provided?