首页 > 解决方案 > 如何从 React Native 中的多个 fireStore 集合中获取数据?

问题描述

我有这些收藏:

我正在使用嵌套循环来拥有一个简单的对象(我不使用 2 个数组)

firebase.firestore().collection("Post").get()
            .then(querySnapshot => {
               querySnapshot.forEach(documentSnapshot => {
                    this.setState({
                        dataPost: this.state.dataPost.concat({
                            ...documentSnapshot.data(),
                            key: documentSnapshot.id,
                            name: this.getUserbyUid(documentSnapshot.data().uid) <- get Name user as soon as I get the uid from the Post collection
                        }),
                    })
              });
      });
  getUserbyUid = (Uid) => {
            firebase.firestore().collection('user').doc(Uid).get()
            .then(documentSnapshot => {
                return documentSnapshot.data().name
            })}

我收到一个错误name: Undefinded

这就是我的想法,我知道有很多更好的方法请帮助我tks

邮政

在此处输入图像描述

用户

在此处输入图像描述

Object {
  "body": "hfdbdb"
  "date": 1614903953253,
  "image": “&quot;https://firebasestorage.googleapis.com/v0/t.....-44¢5-b091-7af310de36d1",
  "key": "IX9KKfcOONSyquUtossTd",
  "name": undefined,
  "uid": "Chc1P3zcWMguc2d2ppKSRrpQx752",
},

目的

标签: react-nativegoogle-cloud-firestore

解决方案


您需要首先检查您的数据结构,因为随着数据库的增长,循环和提取数据会变得非常复杂。

您可以在此处阅读有关不同数据结构的更多信息。每个都有不同的优点和缺点,选择一个将取决于您的用例。

一旦你有一个良好的数据库基础,你的代码就会变得更干净、更容易阅读。

作为 Python 中的示例:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


cred = credentials.ApplicationDefault()

firebase_admin.initialize_app(cred, {
    'projectId': 'test-project',
})

db = firestore.client()

获取用户数据

users = db.collection('User').document('document')

user = users.get()

print(f'Data: {user.to_dict()}')

. . .

Data: {'avatar': 'http://my-avatar-image.com',
       'name': 'My Good Name'}

获取帖子数据

posts = db.collection('Post').document('document')

post = posts.get()
 
print(f'Data: {post.to_dict()}')

. . .

Data: {'date': '121445475589',
       'body': 'sdfsdfsdf',
       'uid': 'Hyhyfd987698IUHGKDSh8d',
       'image': 'http://my-example-image.com'
}

获取集合中的文档

users = db.collection(u'User').stream()

for user in users:
    print(f'{user.id} => {user.to_dict()}')

. . . 

document => {'avatar': 'http://my-avatar-image.com', 'name': 'My Name'}
document1 => {'avatar': 'http://my-avatar-image.com', 'name': 'My Good Name'}

从集合组中获取文档

users = db.collection(u'Users')

a_posts = users.document(u'A').collection(u'post')
a_posts.document().set({
    u'name': u'My Good Name',
    u'type': u'user'
})

b_posts = users.document(u'B').collection(u'post')
b_posts.document().set({
    u'name': u'My Best Name',
    u'type': u'user'
})

c_posts = users.document(u'C').collection(u'post')
c_posts.document().set({
    u'name': u'My Real Name',
    u'type': u'user'
})

names = db.collection_group(u'post')\
    .where(u'type', u'==', u'user')

all_users = names.stream()

for user in all_users:
    print(f'{user.id} => {user.to_dict()}')

可能的错误信息

如果您创建数据库然后尝试直接提取数据,您可能会收到此错误消息。在后台建立索引时等待几分钟,然后重试。

FailedPrecondition: 400 The query requires a COLLECTION_GROUP_ASC index for collection . . That index is not ready yet. 

推荐阅读