簡體   English   中英

根據 Firestore 上的值從數組中刪除 object(地圖)

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

使用此線程作為參考如何從 firestore 中的數組中刪除 object我想進一步了解如何從 firestore 中的數組中刪除 object。

我的firestore設置如下:

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

我正在嘗試根據其值對特定的 object 進行數組刪除。

這是我當前的代碼,盡管閱讀了其他帖子說它應該工作,但它不起作用。

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"))

關於我可以嘗試什么的任何建議? 或任何好的解決方法?

PS 我在我的 React Native 應用程序的客戶端執行此操作,我寧願不必調用端點。

目前尚不清楚如何聲明您在代碼中使用的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"))

您沒有說明您是否正在使用帶有模塊的捆綁器,但以下代碼(沒有捆綁器)可以解決問題:

  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