Home > OS >  removing an object from firestore 9 array using arrayRemove()?
removing an object from firestore 9 array using arrayRemove()?

Time:02-25

I am trying to remove an object from array in in firestore, but encountered an obstacle what are the requirement or the reference to do the removal ? does one key value in the object sufficient to do the remove or should the object by identical to the one that is getting removed ?

const deleteWeek = async () => {
        const docRef = doc(db, 'Weeks', id);
        await updateDoc(docRef, {
          weeks: arrayRemove({
            weekId: '7518005f-7b10-44b6-8e0a-5e41081ee064',
          }),
        });
      };
      deleteWeek();
    }

however week in data base looks like this

{name ,"Week 2"
days : [/*data all kinds*/]
weekId : "7518005f-7b10-44b6-8e0a-5e41081ee064"}

CodePudding user response:

If it's an array of object, then you need to know the whole object to use arrayRemove() For example, if the a document looks like this:

{
  ...data
  weeks: [
    { 
      name: "Week 2",
      days: [/*data all kinds*/]
      weekId: "7518005f-7b10-44b6-8e0a-5e41081ee064"}
    }
  ]
}

You'll have to pass the entire week object in arrayRemove(). It might be better to store such data in sub-collections instead so you can query/delete a specific one.

  • Related