Javascript 找到CollectionGroup时获取完整数据
查询1个集合中的所有数据时出现问题 用户->UidUser->信息用户(帖子(收藏)、姓名、年龄…) 然后,我使用下面的代码获取CollectionGroup(Post),但如何获取用户名和年龄 当我使用两个嵌套循环时,另一个问题是forEach不能在其中使用wait和Async,这会使我丢失数据Javascript 找到CollectionGroup时获取完整数据,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,查询1个集合中的所有数据时出现问题 用户->UidUser->信息用户(帖子(收藏)、姓名、年龄…) 然后,我使用下面的代码获取CollectionGroup(Post),但如何获取用户名和年龄 当我使用两个嵌套循环时,另一个问题是forEach不能在其中使用wait和Async,这会使我丢失数据 getData = async () => { await firebase.firestore().collection('user').doc(Fire.shared.
getData = async () => {
await firebase.firestore().collection('user').doc(Fire.shared.uid).onSnapshot(documentSnapshot => {
firebase.firestore().collectionGroup('Post').where('uid', 'in',
documentSnapshot.data().Followings).get().then(querySnapshot => {
const Post = [];
querySnapshot.forEach(post => {
Post.push({ data: post.data() }) // This is correct and get full data's Post
});
this.setState({ dataPost: Post })
})
})
}
我想你能解决这个问题
firebase.firestore()
.collection("user")
.doc(Fire.shared.uid)
.onSnapshot((documentSnapshot) => {
firebase
.firestore()
.collectionGroup("Post")
.where("uid", "in", documentSnapshot.data().Followings)
.get()
.then((querySnapshot) => {
const Post = [];
querySnapshot.forEach((post) => {
Post.push(new Promise((resolve, reject) => {
firebase
.firestore()
.collection("user")
.doc(post.data().id)
.onSnapshot((user) => {
resolve({ data: post.data(), user: documentSnapshot.data() });
});
}))
});
Promise.all(Post).then(res => {
console.log(res)
this.setState({ dataPost: res})
})
});
});
我用一个简单的setTimeout函数示例来说明它是如何工作的&forEach函数可能在遇到此类问题时对其他人有所帮助
异步函数调用(){
常量nums=[1,2,3,4,5];
常量承诺=[];
数量(res)=>{
设置超时(()=>{
承诺。推动(res*2);
}, 10000);
});
console.log(承诺);
}
call()代码>它不起作用//数组[]我只是更新我的答案你能再检查一下吗非常感谢,我也做过承诺,但没有一个有效。再次感谢你
firebase.firestore()
.collection("user")
.doc(Fire.shared.uid)
.onSnapshot((documentSnapshot) => {
firebase
.firestore()
.collectionGroup("Post")
.where("uid", "in", documentSnapshot.data().Followings)
.get()
.then((querySnapshot) => {
const Post = [];
querySnapshot.forEach((post) => {
Post.push(new Promise((resolve, reject) => {
firebase
.firestore()
.collection("user")
.doc(post.data().id)
.onSnapshot((user) => {
resolve({ data: post.data(), user: documentSnapshot.data() });
});
}))
});
Promise.all(Post).then(res => {
console.log(res)
this.setState({ dataPost: res})
})
});
});