reactjs - 如何访问引用 Firebase 中另一个对象的对象
问题描述
我的 Firebase .json 看起来像:
"Employees": {
"Employee1" : {
"Activities": {
"Activity1": true
},
"age" : 61,
"name" : "Employee1"
},
.
.
.
//other employees
}
"Activities": {
"Activity1": {
"name": "Name of Activity1",
"value": 2,
"cost": 2
},
.
.
.
//other activities
}
我想要做的是显示 Employee1。在这种情况下,Employee1 在活动下只有 Activity1。我需要显示 Employee1 数据,包括其活动的数据。我正在使用 React,我正在使用
firebase.database().ref()
并on('value', function(snapshot))
显示来自 Firebase 的数据。任何帮助将不胜感激。
解决方案
在 NoSQL 数据建模中,您应该毫不犹豫地复制数据,即以这种方式进行非规范化,使您的查询更容易,更重要的是,更快。
因此,在您的情况下,您可以按如下方式修改数据结构:
"Employees": {
"Employee1" : {
"Activities": {
"Activity1": {
"name": "Name of Activity1", // <- add the details of the activity here
"value": 2,
"cost": 2
},
},
"age" : 61,
"name" : "Employee1"
},
.
.
.
//other employees
}
"Activities": {
"Activity1": {
"name": "Name of Activity1",
"value": 2,
"cost": 2
},
这种技术的一个缺点是您必须保持两个节点同步,但这可以通过使用该update()
方法轻松完成,请参阅文档
推荐阅读
- html - 是否可以在画布内的选定区域内填充图像?
- java - 在循环中使用 if 语句?- 加工
- angular - 我们是否需要付费的 firebase/firestore 帐户来实施条带支付?
- wpf - “在 wpf xaml 中使用或不使用 ResourceDictionary 来包装我的资源”有什么区别吗?
- google-chrome - 如何在chrome中的当前标签之后立即打开一个新标签?
- c# - 区分由用户输入触发的事件与。UWP 背后的代码
- javascript - 当从输入框中删除最后一个字符时,事件未在剑道网格和角度 5 中触发?
- mysql - Mysql 文档 WHERE 子句优化:索引使用的常量表达式只计算一次
- linux - 过滤 perf.data (linux perf profiler) 文件的时间间隔
- mod-rewrite - 将移动文件的请求重定向到子目录