Home > Software design >  How to delete a value inside of an array in a firestore doc
How to delete a value inside of an array in a firestore doc

Time:05-26

Have been trying to figure this out for a while to no avail. My file structure is as so:

file structure

Basically, I am trying to create a function that takes a meetingID and AtendeeID, then looks up the document with the corresponding meetingID and deletes the specified attendee from the attendees array in the doc.

I have been playing around with FirebaseFirestore.FieldValue.arrayRemove but am not sure how to adapt it to my situation properly. Any help would be appreciated

For reference, this is where I am currently at:

const removeAttendee = async (meetingID, attendee) => {
  const meetingRef = doc(db, "meetings", meetingID);
  await updateDoc(meetingRef, {
    "attendees": FirebaseFirestore.FieldValue.arrayRemove({"attendees": attendee});
  })
}

Error:

./firebase.js
Error: 
  x Expected ',', got ';'
     ,----
 224 | "attendees": FirebaseFirestore.FieldValue.arrayRemove({"attendees": attendee});
     :                                                                               ^
     `----

Caused by:
    0: failed to process input file
    1: Syntax Error

CodePudding user response:

The code is good, it's just a typo, as the error message suggests, you used a semi-colon in your update object, simply remove it and the code will work just fine!

await updateDoc(meetingRef, {
    "attendees": FirebaseFirestore.FieldValue.arrayRemove({"attendees": attendee}) // here, remove the `;` optionally you can add a coma instead 
  })

Welcome to SO and happy coding.

  • Related