Farid
Farid

Reputation: 59

How to keep data from a Firestore Query in a variable?

I can't get the values of apps in a query in Firestore. I want to render with EJS the values of the groups and the apps collection but after the THEN from firestore the values of apps get deleted and I cant use them after.

I tried with lodash const appsClone = _.cloneDeep(apps); but no success.

The apps collection is inside the groups collection.

Here is the example of what I'm trying to do:

let groups = [];  
let apps = [];
const db = admin.firestore();
const group = db.collection('groups').where('roleId', '==', req.session.idRol);
      group.get()   // Trying to get all the groups
      .then(snapshot => {
           snapshot.docs.forEach(grp => {                        
           groups.push(grp.data());  // I can get all the groups without any problem
           grp.ref.collection('apps').get() // once I get the groups I try to get the APPS collection inside every group
           .then(snapshot2 => {
                 snapshot2.docs.forEach(app => {
                    apps.push(app.data()); // I can get the values of app and apps without any problem
                    console.log(apps); // I can get the value here of apps still
                  })
    // Now I cant get the value 
    // And I cant render here because I need the missing groups in the last forEach
            })                        
     });
     console.log("APPS: " + apps);   // Here apps is empty
     res.render("pages/launchpad", { groups, apps }); // Get to render groups but varible apps is empty.
     })
     .catch(err => {
          console.log(err);
     })
     });
     })
     .catch(err => {
        console.log(err);
     })

I really appreciate the help, have a good day.

Upvotes: 0

Views: 1587

Answers (1)

ittus
ittus

Reputation: 22403

console.log("APPS: " + apps); is executed before your promise is resolved.

You should wait for all promises are resolved before calling res.render. This can be done by using Promise.all

let groups = [];
let apps = [];
const db = admin.firestore();
const group = db.collection('groups').where('roleId', '==', req.session.idRol);
group.get() // Trying to get all the groups
    .then(snapshot => {
        return Promise.all(snapshot.docs.map(grp => {
            groups.push(grp.data()); // I can get all the groups without any proble
            return grp.ref.collection('apps').get() // once I get the groups I try to get the APPS collection inside every group
                .then(snapshot2 => {
                    snapshot2.docs.forEach(app => {
                        apps.push(app.data()); // I can get the values of app and apps without any problem
                        console.log(apps); // I can get the value here of apps still
                    })

                })
        }))
        .then(() => {
            console.log("APPS: " + apps); 
            res.render("pages/launchpad", {
                groups,
                apps
            });
        })

    })
    .catch(err => {
        console.log(err);
    })
});
})
.catch(err => {
    console.log(err);
})

Upvotes: 2

Related Questions