简体   繁体   English

无法在 firebase/firestore 的数组中删除具有特定值的 object

[英]Can't delete an object with a certained value inside an array in firebase/firestore

So I am a building a member system and every member has an array points.所以我正在建立一个会员系统,每个会员都有一个数组积分。 But I can't seem to delete an object when a person tries to remove this point.但是当有人试图删除这一点时,我似乎无法删除 object。

users(collection) -
 member(document) -
      "id" . --fields
      "username" . --fields
      "userevents" . --array
         [0]
            "id"
            "date"
            "price"
         [1]
            "id"
            "date"
            "price"




deletePoint = (userId, pointId, date, price) => {
    let docRef = this.db.collection('users').doc(userId);
    docRef.update({
      points: this.db.FieldValue.arrayRemove({
        date: date,
        id: pointId,
        price: price,
      }),
    });
  };

this.db is firebase.firestore() this.db 是 firebase.firestore()

According to the documentation when you're using arrayRemove you have to point directly to the field that you want to remove, not the content within said field so your example would become:根据使用 arrayRemove 时的文档,您必须直接指向要删除的字段,而不是所述字段中的内容,因此您的示例将变为:

deletePoint = (userId, pointId, date, price) => {
    let docRef = this.db.collection('users').doc(userId);
    docRef.update({
      points: this.db.FieldValue.arrayRemove("0"),
    });
  };

And this will delete the whole content of the field (id, date, price) with an index of 0.这将删除索引为 0 的字段(id、日期、价格)的全部内容。

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

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