简体   繁体   中英

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.

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

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:

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.

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