【发布时间】:2021-01-15 13:07:00
【问题描述】:
我有一个集合,它在单个项目中也有子集合。我过滤集合,然后尝试访问过滤后的第一项的子集合。我尝试使用 foreach 循环来做到这一点。但是没有更简单的直接方式,例如给出直接路径吗?我该怎么做?
示例数据路径通过:/users/O2Am1XSXBOOWOQj2pzyu/appointments
这是我的代码:
useEffect(()=>{
var currentUser = auth().currentUser;
if (currentUser) {
setFoundUser(currentUser);
firestore().collection('users').where('phone','==',currentUser.phoneNumber).get()
.then(function (querySnapshot) {
if(!querySnapshot.empty){
setFoundUser(querySnapshot.docs[0])
firestore().collection('users/'+querySnapshot.docs[0].id+'/appointments').get()
.then(function (appointmentsSnapshot) {
var appointmentsArray=[]
if(appointmentsSnapshot!=undefined && !appointmentsSnapshot.empty){
console.log('say: '+appointmentsSnapshot.docs.length)
appointmentsSnapshot.forEach(function(s){
firestore().doc(s.data().aref).get()
.then(function (appiSnapshot) {
if(appiSnapshot!=undefined&&!appiSnapshot.empty){
console.log('appi: '+appiSnapshot.docs.length)
appointmentsArray.push({id:appiSnapshot.id, data:appiSnapshot.data()})
}
});
setAppointments(appointmentsArray)
});
}
});
}
});
}
},[])
【问题讨论】:
-
请编辑问题以解释此代码的工作方式与您期望的不同,或者提供更多关于您遇到的具体问题的详细信息。请记住,我们看不到您的数据库或代码中的任何变量值。
-
我用foreach循环来做有没有更方便的方法?
-
如果您解释一下您的最终目标,并说明您希望使用的数据,这将很有帮助。仔细观察几十行代码可能很难弄清楚它要做什么。
标签: javascript firebase react-native google-cloud-firestore