【问题标题】:REACT NATIVE FIRESTORE SUBCOLLECTION PROBLEMReact Native Firestore 子集合问题
【发布时间】:2021-08-13 16:01:50
【问题描述】:

我正在尝试检索 firestore firebase 子集合。

下面这段代码一切正常:

  await firestore()
    .collection('VILLES')
    .doc('9HNkAv99pd1rm9Q3vdwd')
    .collection('restaurents')
    .get()
    .then(querySnapshot => {
      querySnapshot.forEach(doc => {
        const {image, address, description, title, ratings, contact} =
          doc.data();

        list.push({
          id: doc.id,
          image: image,
          address: address,
          description: description,
          title: title,
          ratings: ratings,
          contact: contact,
        });
      });
    });
  setrestaurents(list);
  if (loading) {
    setloading(false);
  }
  console.log('restaurents:', restaurents);
} catch (e) {
  console.log(e);
}

但问题是我有很多文件都有个人收藏 我如何编写代码以便每个文档都能检索自己的子集合,并且我知道问题出在文档 Id 上

我试过了

  await firestore()
    .collection('VILLES')
    .doc(VILLESId)
    .collection('restaurents')
    .get()

但我收到此错误

可能的未处理承诺拒绝(id:0):
TypeError: undefined is not an object (evalating '_ref2.VILLESId')

【问题讨论】:

    标签: javascript react-native google-cloud-firestore


    【解决方案1】:

    如果您想从所有restaurents 子集合中检索所有文档,您可以像这样使用collection group query

      firestore()
        .collectionGroup('restaurents')
        .get()
        .then(querySnapshot => {
          querySnapshot.forEach(doc => {
            ...
    

    这将从所有restaurents 集合中获取所有文档。


    如果您需要知道上述特定restaurent 文档的VILLE 文档ID,您可以通过以下方式获取:

    doc.ref.parent().parent().id
    

    【讨论】:

    • 这不是我要查找的内容 我有一个集合名称城镇并且这个集合有文档可以说我们有 5 个文档并且每个文档都有一个子集合问题是当我选择一个特定文档时我将如果我回来并选择另一个文档,我想重新获取属于此文档的子集合。我想重新获取他自己的子集合。为了测试我的代码,我复制一个文档 ID 并传递它,但如果我使用此代码,我无法获得第一个集合的其余文档感谢您的帮助
    • 请不要输入全部大写,因为这会降低可读性并且通常被认为是大喊大叫。
    • 很抱歉我不知道。对不起
    • @GroupeAkoua 请参阅another thread 回答此问题的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2021-12-02
    • 2020-11-11
    相关资源
    最近更新 更多