【发布时间】:2020-08-15 12:33:51
【问题描述】:
我正在使用带有 express 的节点服务器并尝试从 firestore 数据库中获取数据。我的数据库结构如下
-Resource Collection
- Documents with fields
- comments subcollection
- documents with fields
- subresource subcollection
- documents with fields
我正在尝试将上述所有数据映射到单个数组中,其中子资源和 cmets 嵌套在单个数组中。以下是我的代码。日志在一个数组中有正确的数据,但是当我发回响应时,它只显示 1 个资源,而且每次都是随机的。收集完所有数据后,promise.all 不应该阻止执行吗?我是否正确使用了承诺?
response.set('Cache-Control','public, max-age=300, s-maxage=600');
db.collection('Resources').get()
.then(snapshot => {
let Resources = snapshot.docs.map(doc => {
var promises = [];
let documentData = doc.data();
documentData['id'] = doc.id;
promises.push(db.collection('Resources').doc(documentData.id).collection('SubResources').get()
.then(snapshot => {
documentData['subresources'] = snapshot.docs.map(doc => {
let subResourceData = doc.data();
subResourceData['id'] = doc.id;
return subResourceData;
})
}));
promises.push(db.collection('Resources').doc(documentData.id).collection('Comments').get()
.then(snapshot => {
documentData['comments'] = snapshot.docs.map(doc => {
return doc.data();
})
}));
Promise.all(promises).then(function(){
console.log(documentData);
return response.json(documentData);
})
})
})
.catch(err => {
console.log('Error getting documents', err);
});
});
【问题讨论】:
-
我不建议对数据库进行规范化,但恰恰相反,对它进行去异常化。
-
记住firebase是一个faas
-
我解释说,根据您的数据模型,您必须关注视图中信息的表示,无论数据是否冗余。因为firebase是一个faas。
-
在“资源集合”中最关键的 cmets 和子资源数据在视图中表示。
-
您的 Promise.all() 仅捕获获取子资源和 cmets 的承诺,而不是“资源”本身。所以对于哪个 Resource 它可以先完成获取子资源和 cmets,它会解析并返回数据。
标签: node.js firebase express promise google-cloud-firestore