Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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"}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading