-1

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)
                      });

                    }

                    
                });

            }

        });
    }

},[])
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Serhat Koroglu
  • 1,263
  • 4
  • 19
  • 41
  • 1
    Please edit the question to explain what isn't working the way you expect with this code, or give some more detail about what specifically you're stuck on. Keep in mind that we can't see your database or really any of the values of the variables in your code. – Doug Stevenson Sep 29 '20 at 21:20
  • i do it with a foreach loop is there any more convenient way? – Serhat Koroglu Sep 29 '20 at 21:26
  • It would be helpful if you explain your end goal, and say something about the data you expect to work with. It can be difficult to stare down dozens of lines of code to figure out what it's trying to do. – Doug Stevenson Sep 29 '20 at 22:28

1 Answers1

2

You will need to perform a loop to get the subcollection data from your collection and the best way is indeed a forEach loop. As indicated in the official documentation here, the way you are doing it's pretty similar to yours. An example of code on how to do that, in case you know the document you want to get the subcollection is the following.

var query = db.collection("users").where("field", "==", "O2Am1XSXBOOWOQj2pzyu");
query.get().then((querySnapshot) => {
  querySnapshot.forEach((document) => {
    document.ref.collection("appointments").get().then((querySnapshot) => {
      ...
    });
  });
});

The query is pretty similar to yours, only reduced a couple of lines that you wouldn't need. This code was based in the answer provided by a Firebase Engineer from Google, that you can check here.

To summarize, you are doing what it's right and recommended for subcollections, as they are not so simple to work with adn you need to follow these specific requirenments to get the information you need. You can also read more details about subcollections and getting their information on this nice article here.

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22