我想获取嵌套数据

时间:2020-10-27 09:03:12

标签: node.js express vue.js google-cloud-firestore

我的数据库结构包括集合和集合属于的子集合

company ->orders->projects->opr->groups

projects opr groups 我为创建树编写了这段代码。Console.log(projectsTree)给我值,但是response.send(projectsTree)仅发送projects值,而没有opr and groups值。我尝试使用await,async但我不知道为什么它不起作用。

     app.get('/getProjectsTree', async (request, response) => {
        response.set('Access-Control-Allow-Origin','*')
          let projectsTree=[{
            label:'',
            children:[{
              children:[{
              }]
            }]
            }];
          let projects =await db.collectionGroup('projects').where('no','==','10343').get();
          if (projects.empty) {
            console.log('No matching documents.');
            return;
          }  
          projects.forEach(async(doc) => {
            
            projectsTree[0].label=doc.data().no;
            let opr =await db.collectionGroup('opr').get();
            if (opr.empty) {
              console.log('No matching documents.');
              return;
            }  
            else{
              opr.forEach(async(doc) => {
                console.log(doc.id, '=>', doc.data());
                projectsTree[0].children.push({label:doc.data().no})
                let groups =await db.collectionGroup('groups').get();
                if (groups.empty) {
                  console.log('No matching documents.');
                  return;
                }
                else
                {
                  groups.forEach(async(doc)=>{
                    projectsTree[0].children[0].children.push({label:doc.data().no})
                  })
                }  
    
              });
            }
          });
      response.send(projectsTree)
})

我想要这样的东西。

 project: [ 
    {
      label: '10343',
      children: [
        {
          label: '10343-01',
          children: [
            { label: '10343-01-01' },
          ]
        },
        
      ]
    }
  ]

0 个答案:

没有答案