I have a collection which also has subcollections within individual items. I filter the collection then trying to access the subcollection of the filtered first item. I tried to do that with a foreach loop. But isn't there an easier direct way, such as giving direct path? How can I do that?
Sample data path is through: /users/O2Am1XSXBOOWOQj2pzyu/appointments
Here is my code:
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)
});
}
});
}
});
}
},[])