【发布时间】:2018-08-08 06:09:33
【问题描述】:
更新:感谢大家的帮助
我从 Firestore 获得 2 个集合:角色和应用程序。 Apps 是 Roles 的子集合,因此它包含在 Roles 集合中,因此,要获取应用程序,我需要首先获取包含它们的 Roles 文档。我这样做没有问题,但我需要保存它们,因为它们存储在 Firebase 中以便以后对它们进行分组,我不知道如何。我想要一些这样的数据:
[
{
roleDescription: 'System Administrator',
active: true,
roleId: '1'
apps: [
{
appId: '2',
appDescription: 'Manage Roles',
groupId: '1',
route: 'roles',
appName: 'Roles',
active: true
},
{
appId: '1',
appDescription: 'Users',
groupId: '1',
route: 'users',
appName: 'Users',
active: true
},
{
...
}
]
},
{
active: true,
roleId: '2',
roleDescription: 'Employee',
apps: [
{
appId: '5',
appDescription: 'Upload Data',
groupId: '1',
route: 'roles',
appName: 'Roles',
active: true
},
{
appId: '1',
appDescription: 'Users',
groupId: '1',
route: 'users',
appName: 'Users',
active: true
},
{
...
}
]
}
]
目前我有这段代码,我可以在其中获取快照中的所有角色并将它们映射以单独获取每个角色,然后使用快照 2 获取包含在该角色中的应用程序也单独将快照 2 中的每个应用程序分配给对象或者也包含在一个数组中的角色数组。
这是我的代码:
ref.get()
.then(function(snapshot) {
return Promise.all(snapshot.docs.map(doc => {
var AppRole = {};
AppRole.role = doc.data();
roles.push(AppRole);
return doc.ref.collection('apps').get()
.then((snapshot2) => {
return snapshot2.docs.map(app => {
roles[count].apps = app.data(); // Here I need to push app.data() to roles[count].apps and problem solver but I don't know how to do it (roles[count].apps is an object and I know it doesnt have the push method but I tried with a counter also like roles[count].apps[i] = app.data(); i++; but no success )
})
})
console.log(count);
count++;
}))
.then(() => {
console.log(roles);
res.render("pages/roles", { name: req.session.name, idiom: req.session.idiom, roles: roles, apps: apps, pageInfo: req.session.lang.roles});
})
【问题讨论】:
-
您能否更详细地解释一下您到底想要实现什么?无论是从您的介绍还是从您的代码中,都不容易理解。
-
我编辑了问题,感谢您的回复!
标签: javascript arrays node.js firebase google-cloud-firestore