react-native - 如何从 React Native 中的多个 fireStore 集合中获取数据?
问题描述
我有这些收藏:
- 具有名称和图像的用户(Uid = 文档),
- Post(Uid Post = doucment) 包括body、image和Uid(Uid用户)
我正在使用嵌套循环来拥有一个简单的对象(我不使用 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": “"https://firebasestorage.googleapis.com/v0/t.....-44¢5-b091-7af310de36d1",
"key": "IX9KKfcOONSyquUtossTd",
"name": undefined,
"uid": "Chc1P3zcWMguc2d2ppKSRrpQx752",
},
解决方案
您需要首先检查您的数据结构,因为随着数据库的增长,循环和提取数据会变得非常复杂。
您可以在此处阅读有关不同数据结构的更多信息。每个都有不同的优点和缺点,选择一个将取决于您的用例。
一旦你有一个良好的数据库基础,你的代码就会变得更干净、更容易阅读。
作为 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.
推荐阅读
- php - 仅以小写形式获取参数值 - PHP phalcon
- python - 如何有效地匹配 Python 中的两个数组值?
- c# - 将类实现从 asp.net mvc 移植到 asp.net mvc 核心
- amazon-web-services - 是否可以从 Mulesoft 调用 AWS Step Function?
- javascript - 如何在 gatsby 的布局文件中获取路径名
- javascript - onclick 只执行第一个函数
- angular - RouterLinkActive 与 *ngFor 一起使用时不起作用
- leaflet - 使用 GeoJson 数据更改传单地图中的标记
- machine-learning - 如何在不平衡的数据集上执行类平衡
- c++ - 如何将字符串转换为cpp中的浮点数