我的 Firebase Firestore 具有以下结构:
是否可以只查询第二张图片中间列中显示的文档 ID?这是我当前的查询,在我看来这是最合乎逻辑的方法。 console.log 返回成功信息,但数组为空:
async function getListofPosts() {
try{
return await useFireStore
.collection('chats')
.doc(`${postId}`)
.collection('chat')
.get().then(res => {
let theMessageList = [];
res.forEach(doc => {
theMessageList.push(doc.data(), doc.id )
});
setMessageList(theMessageList);
});
}
catch {
console.log('error on getListofPosts')
}
finally {
console.log('getListofPosts worked')
}
}
非常感谢任何帮助。 干杯,马特
答案 0 :(得分:1)
相关问题 - Firestore Cloud Function empty collection
您在集合 chats
和 chat
下的文档似乎是空的(文档 ID 以斜体显示)。所以要解决这个问题,您应该使用 collectionGroup
查询。
return await useFireStore
.collectionGroup('chat')
.get().then(res => {
let theMessageList = [];
res.forEach(doc => {
theMessageList.push(doc.data(), doc.id )
});
setMessageList(theMessageList);
});
在使用 collectionGroup
查询之前,我建议您阅读此查询及其此处的限制 - https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query