1

Using this thread as a reference How to delete object from array in firestore I wanted to take this a little further and understand how to delete an object from a array in firestore.

I have my firestore setup like such:

Users(collection) -> 12345(document) -> myArray:[{x:"hello", y:"please"}, {x:"hello", y:"thanks"}]

I am trying to arrayRemove a specific object based on its values.

This is my current code and it does not work despite reading other posts that say it should.

import firestore from '@react-native-firebase/firestore'; //EDIT

 let obj = {x:"hello", y:"thanks"}
            firestore()
                .collection("Users")
                .doc("12345")
                .update({
                    myArray: firestore.FieldValue.arrayRemove(obj),
                }).then(r =>console.log("YEAAAH"))

Any suggestions on what I could try? or any good workarounds?

P.S. I am doing this on client-side of my React Native app and I would prefer not having to call an endpoint.

Fabrizio Botalla
  • 697
  • 1
  • 8
  • 25
  • 1
    Which error do you get? Are you sure that `firestore().collection("Users")...` is correct? shouldn't it be `firebase.firestore().collection("Users")...` – Renaud Tarnec Feb 04 '21 at 13:15
  • I don't get any errors.. it just does not do anything at all. I edited my question to give some more context. – Fabrizio Botalla Feb 04 '21 at 20:29
  • Did you refer to the documentation [Update Elements In An Array](https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array) also see a Firebasers answer [here](https://stackoverflow.com/questions/60905371/remove-element-from-firestore-array). Also, there are a LOT of [duplicate questions](https://stackoverflow.com/questions/62675759/how-to-remove-an-element-from-an-array-in-multiple-documents-in-firestore) – Jay Feb 04 '21 at 22:24
  • @Jay Thank you for the references. I took a look at all 3 and I am doing basically the same thing as the 2nd reference, but I am trying to understand why it does not work. The last reference is referring to something different than what I am trying to do.. I am removing an obj not an element. – Fabrizio Botalla Feb 04 '21 at 22:36
  • An element and object in an array are used interchangeably as they both refer to the 'thing' stored at an array index. They're also called an array element or aka item. See [Arrays](https://www.webopedia.com/definitions/array/). It's important because in Firestore, you have to match the object/element/item/thing exactly to remove it per the above link #2 – Jay Feb 04 '21 at 23:36
  • @jay Sure thing, still the point is still valid of what am I doing wrong in code? I am following what your references are suggesting to do. To my understanding I am matching it exactly, but it does not remove it. – Fabrizio Botalla Feb 04 '21 at 23:39

1 Answers1

3

It is not clear how you declare the firestore() method and the firestore variable that you use in your code:

firestore()   <= What exactly is firestore()?
  .collection("Users")
  .doc("12345")
  .update({
      myArray: firestore.FieldValue.arrayRemove(obj),  <= What exactly is firestore?
  })
  .then(r =>console.log("YEAAAH"))

You don't indicate if you are using a bundler with modules, but the following code (without bundler) will do the trick:

  var config = {
     ....
  };

  firebase.initializeApp(config);

  let obj = { x: 'hello', y: 'thanks' };
  firebase.firestore()    // Note the difference
    .collection('Users')
    .doc('12345')
    .update({
      myArray: firebase.firestore.FieldValue.arrayRemove(obj),  // Note the difference
    })
    .then((r) => console.log('YEAAAH'));
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thank you for helping out! I edited my question with how I import my firestore module. I used it that way in other parts of my code for example firestore().collection('Users').doc(auth().currentUser.uid).update({ followingThreads:firestore.FieldValue.arrayRemove(data.docRefId) }) And this works. but as you can see the array is not made up of objects. – Fabrizio Botalla Feb 04 '21 at 20:25
  • @IlllIl Arrays are only made up of objects. A String is an object, and Int is an object and another array is an object. In your array this `{x:"hello", y:"please"}` is an object – Jay Feb 04 '21 at 23:30
  • 1
    Based on you comments here and above, it seems that you don’t succeed in understanding why the object is not removed from the array. I would suggest you first try with a simple HTML page (without bundle and modules): use the code from my answer, it does work and check if it works in your case. Another thought: are you sure you use maps (objects) in an array? Can you share a screenshot of your Firestore DB. – Renaud Tarnec Feb 05 '21 at 04:54
  • Hey @IlllIl did you have time to further look at the proposed solution? – Renaud Tarnec Feb 07 '21 at 13:35
  • @RenaudTarnec I got it working. My project was pointing at the prod-env instead of the dev-env. I do totally understand how that works, that's why the confusion. Thanks for the help anyway. – Fabrizio Botalla Feb 07 '21 at 20:38