我的数据库结构包括集合和集合属于的子集合
company ->orders->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' },
]
},
]
}
]