Fabrizio Botalla
Fabrizio Botalla

Reputation: 742

Removing an object (map) from array based on its values on firestore

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.

Upvotes: 1

Views: 730

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

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'));

Upvotes: 3

Related Questions