json - 从 Firestore 获取子集合的 JSON 响应
问题描述
我正在尝试合并所有医生的信息:
{"id": "...", "firstName": "...", "lastName": "..."}
我的 Firestore 数据树如下所示:
我正在写我GET
的:
app.get('/doctors/cardiology/doctors', async (req, res) => {
try {
const doctorsDetailsQuerySnapshot = await db.collection("doctors").doc("cardiology").listCollections();
const doctors: any[] = [];
doctorsDetailsQuerySnapshot.forEach((doc1) => {
doctors.push({
id: doc1.id,
data: doc1.doc(doc1.id).collection(doc1.id),
});
});
res.status(200).json(doctors);
} catch (error) {
res.status(500).send(error);
}
实际输出:
[
{
"id": "919505294523",
"data": {
.... metadata....
}
},
{
"id": "919505294525",
"data": {
.... metadata....
}
}
]
预期输出:
[
{
"id": "919505294523",
"data": {
"id": "919505294523",
"firstName": "B.F.",
"lastName": "Skinner"
}
},
{
"id": "919505294525",
"data": {
"id": "919505294525",
"firstName": "Carl",
"lastName": "Rogers"
}
}
]
有人可以帮助我的新手头脑吗?几乎卡了2个小时。
解决方案
作为您有关数据库结构的问题的答案。
现在你的结构是这样的:
\collection(doctors)
\document(specialization)
\collection(doctorId)
\document(doctorID)
因此,您有一个集合,doctors
其中包含每个专业的文档(例如cardiology
),并且在每个专业下,您有每个医生的集合,其中包含带有医生字段的文档。
相反,我会建议一个更简单、更直接的结构,如下所示:
\collection(Doctors)
\document(doctorID)
我的建议是制作一个集合,Doctors
其中包含每个医生的文档。在文档的字段中,您可以包含已经存在的字段(firstName、id、lastName)并添加更多字段specialization
(例如“专业:心脏病学”)。
这是一个更简单的实现,您仍然可以使用where('specialization', '==', 'cardiology')
. where()
可以在此处找到有关该方法的更多信息
推荐阅读
- java - PSQLException:错误:关系“床”的“idRoom”列中的空值违反非空约束
- azure - 尝试使用 Java 连接到 Microsoft SQL Server 时出现 AssertionError
- ios - 在 SwiftUI 应用程序中更改背景和前景颜色的默认颜色
- python - 点到框边界的距离
- django - 为什么小部件 EmailInput 样式不适用
- javascript - 如何在网页上实现 javascript 模块化?
- mysql - MySQL 导入 - CSV - 文件拒绝正确导入
- java - 进度条 Android Studio Java
- reactjs - 在 React JS 中使用 Fluent UI Checkbox CSS
- d3.js - 在 D3 / Observable 中的 API 调用中使用 fetch 时出错