简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM