简体   繁体   English

根据 Firestore 上的值从数组中删除 object(地图)

[英]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.使用此线程作为参考如何从 firestore 中的数组中删除 object我想进一步了解如何从 firestore 中的数组中删除 object。

I have my firestore setup like such:我的firestore设置如下:

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.我正在尝试根据其值对特定的 object 进行数组删除。

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. PS 我在我的 React Native 应用程序的客户端执行此操作,我宁愿不必调用端点。

It is not clear how you declare the firestore() method and the firestore variable that you use in your code:目前尚不清楚如何声明您在代码中使用的firestore()方法和firestore变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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