In the following example I am updating 2 things. The first thing is an existing array in the db, and the second thing is an existing object. The array pushing is working as it should, but the object has an issue. I mean is there an equivalent for array union to object?
// Doc model current state before the incoming update.
data
{items :[]}
{lists : {}}
const addObjToClientsArr = async () => {
const docRef = doc(db, 'data', _authContext.currentUser.uid);
const payload = selected;
// Array
await updateDoc(docRef, { items: arrayUnion(payload) });
// Obeject
const payload2 = {[context.id]: [
{ a : 1 },
]}
updateDoc(docRef, {
lists: {
payload2
});
};
It sounds like you want to update a field inside a nested object, which you can do with:
const path = `lists.${context.id}.a`;
await updateDoc(docRef, {
[path]: 1
})