Home > Blockchain >  How to create and push an object in to firebase array?
How to create and push an object in to firebase array?

Time:02-20

My current logic is the following creating adding a document with a user id in to users collection, and push the first object, then on the second call I want to be able to update the excising array with a new object.

const docRef = doc(db, 'user', _authContext.currentUser.uid);
const payload = { items: [{item1 : 1}] };
await setDoc(docRef, payload);


// New array after update (second call of the function)
items: [{item1 : 1}, {item2 : 2}]

CodePudding user response:

I think you're looking for:

await updateDoc(docRef, { items: arrayUnion({ item2: 2 }) });

Also see the Firebase documentation on adding items to an array.

  • Related