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.

6
  • 1
    Which error do you get? Are you sure that firestore().collection("Users")... is correct? shouldn't it be firebase.firestore().collection("Users")... Commented Feb 4, 2021 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. Commented Feb 4, 2021 at 20:29
  • Did you refer to the documentation Update Elements In An Array also see a Firebasers answer here. Also, there are a LOT of duplicate questions Commented Feb 4, 2021 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. Commented Feb 4, 2021 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. It's important because in Firestore, you have to match the object/element/item/thing exactly to remove it per the above link #2 Commented Feb 4, 2021 at 23:36

1 Answer 1

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'));
Sign up to request clarification or add additional context in comments.

5 Comments

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.
@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
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.
Hey @IlllIl did you have time to further look at the proposed solution?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.