如何从Firestore中的数组删除对象 [英] How to delete object from array in firestore

查看:55
本文介绍了如何从Firestore中的数组删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从firestore中的Array中删除Object时遇到问题. 我在Firestore中有以下数据:

I have a problem with deleting an Object out of an Array in firestore. I have this data in firestore:

现在我想从posts Array中删除第二个Object.

And now I would like to delete e.g the second Object out of the posts Array.

代码:

 deletePic () {
  let docId = `${this.currentUser.uid}`

   fb.usersCollection.doc(docId).update({
     posts: firebase.firestore.FieldValue.arrayRemove()
   })
  .catch(function(error) {
      console.error("Error removing document: ", error);
  });
}

但是我不知道如何定义arrayRemove()

But I do not know how to define arrayRemove()

这些是图片,每个图片都有一个删除按钮以删除图片.

These are the pictures and each one has a delete button to delete the picture.

推荐答案

您不能使用

Can't you use filter ? And then return the new posts array to your fb.usersCollection method

//deleteId is the id from the post you want to delete
posts.filter(post => post.id !== deleteId);

edit:所以应该是这样的:

edit : So This should be something like :

 deletePic (deleteId) {
  let docId = `${this.currentUser.uid}`

   //deleteId is the id from the post you want to delete

   fb.usersCollection.doc(docId).update({
     posts: posts.filter(post => post.id !== deleteId);
   })
  .catch(function(error) {
      console.error("Error removing document: ", error);
  });
}

这篇关于如何从Firestore中的数组删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆