【问题标题】:Pushing array into object that is contained in array将数组推送到数组中包含的对象中
【发布时间】: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


【解决方案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});
    })

【讨论】:

  • 我想我实际上得到了我想要的,但是 role.apps 在他们被映射的地方出现了未定义,试图在最终地图后面添加一个承诺,但没有运气!知道为什么吗?谢谢! @cal2u
  • 忘记了 map() 的返回值。让我知道这是否能解决问题。
  • 已修复!非常感谢,这看起来很简单,但我对 JavaScript 有点陌生,祝你有美好的一天!
【解决方案2】:

并不真正了解代码的全部范围,但为什么不创建一个将应用分配给角色的对象,并将最终结果存储在 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});
    })

【讨论】:

  • 这对我帮助很大!但是 app.data() 是单个应用程序而不是数组,这就是为什么我映射 snapshot2.docs 以获取所有应用程序然后我想将每个 app.data() 推送到角色 [count].apps 但我找不到还有一种方法,感谢您的宝贵时间!
猜你喜欢
  • 2021-07-20
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多