【问题标题】:How to Delete a single subcollection from the firestore database collection?如何从 Firestore 数据库集合中删除单个子集合?
【发布时间】:2021-07-29 16:49:14
【问题描述】:

我正在尝试从图像列表中删除单个图像。目前,它正在完全删除 postImg 字段,但是,我正在尝试从列表中删除单个图像, 这是我的尝试,

const handleDelete = async postId => {
    const deleteImg = await firestore()
      .collection(`${user.email}`)
      .doc(postId)
      .update({
        postImg: firestore.FieldValue.delete(),
      });
    console.log(deleteImg);
  };

这里id = 'FcXwUNg92k9i21LAWGZo'

<Icon
  name="trash-outline"
  size={28}
  color="white"
  onPress={() => handleDelete(id)}
/>

这是我的数据库,

【问题讨论】:

    标签: reactjs firebase react-native google-cloud-firestore


    【解决方案1】:

    postImg 字段包含一个数组,因此您需要从数组列表中删除特定元素。 要从 Firestore 中的数组中删除,请使用 arrayRemove()

    const reference = db.collection(user.email).doc(postId);    
    reference.update({
        postImg: firebase.firestore.FieldValue.arrayRemove(<full_image_path_to_remove>)
    });
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多