将数组推入数组中包含的对象

时间:2018-08-08 06:09:33

标签: javascript arrays node.js firebase google-cloud-firestore

已更新:感谢大家的帮助

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

2 个答案:

答案 0 :(得分:1)

不是真的了解代码的全部范围,但是为什么不创建一个将应用分配给角色的对象,并将最终结果存储在roles数组中呢?

在这种情况下,您不需要j计数器。我可以想象它看起来像这样:

let roles = [];
const count = 0;
const db = admin.firestore();
const ref = db.collection('roles');

ref.get()
.then(function(querySnapshot) {
    return Promise.all(querySnapshot.docs.map(doc => {
        var AppRole = {};   //Declare AppRole object
        AppRole.role = doc.data();   //Based on your question, this should return one role
        roles.push(AppRole);  //Adds object to array (You can use 'roles[count] = AppRole' if you feel more comfortable)
        return doc.ref.collection('apps').get()
        .then((snapshot2) => {
            return snapshot2.docs.map(app => {
                roles[count].apps = app.data(); //Based on your question, this should return an array of apps. That array should be stored within that particular object.
            })
        })
        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});
    })

答案 1 :(得分:1)

您应该能够在没有任何计数器的情况下执行此操作。我已经评论了代码中的主要更改。

基本上,创建角色后只需保留对角色的引用。然后,您可以使用map()一次为其分配应用程序列表。

const roles = []; // You can add and remove elements to a const array
const db = admin.firestore();
const ref = db.collection('roles');

ref.get()
.then(function(roleQuery) {
    return Promise.all(roleQuery.docs.map(roleDoc => {
        // ** Create a new role and keep a reference to it **
        const role = roleDoc.data()
        roles.push(role);
        return roleDoc.ref.collection('apps').get()
        .then((appQuery) => {
            // ** Easily create a new array by calling 
            // ** map() on the app documents
            role.apps = appQuery.docs.map(appDoc => {
                return appDoc.data();
            })
            // ** alternate syntax: 
            // ** appQuery.docs.map(appDoc => appDoc.data())
        })
    }))
    .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});
    })