如何将来自Firestore查询的数据保留在变量中? [英] How to keep data from a Firestore Query in a variable?

查看:43
本文介绍了如何将来自Firestore查询的数据保留在变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Firestore中的查询中获取应用程序的值。我想用EJS渲染组和apps集合的值,但是从firestore中的THEN之后,app的值被删除了,之后我就不能使用它们了。

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.

我尝试了lodash const appsClone = _.cloneDeep(apps); ,但没有成功。

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

应用程序集合在groups集合内。

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.

推荐答案

console.log( APPS: +应用程序); 在您的诺言兑现之前就已执行。

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

您应该等待所有的承诺都解决后再调用 res.render 。可以使用 Promise.all

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

这篇关于如何将来自Firestore查询的数据保留在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆