javascript - 使用 React Native 连接来自 firebase 的表
问题描述
嗨,我是 React Native 的新手,我正在制作一个管理社团的应用程序,我在我的 firebase 数据库中添加了“部门”和“社团”表,现在我想以这样一种方式加入这些表,如果我从中删除值部门然后我各自的社会反对者它也被删除了。
这是我的删除部门功能,删除社团的乐趣也一样;
deleteDept = () => {
var abcd = deptref
.orderByChild('uuid')
abcd.once('value', function(abcdSnap) {
var a = abcdSnap.val();
var b = Object.keys(a)[0];
deptref.child(b)
.remove()
.then(function() {
console.log('Remove succeeded.');
alert('Department Removed ');
this.setState({
isDialogVisible: false
});
})
.catch(function(error) {
console.log('Remove failed: ' + error.message);
});
});
};
解决方案
您无法像对待 RDBMS / SQL 数据库那样考虑 firebase realtime。将 firebase realtime 视为一个大型 JSON 对象,通过对象键(也是 URL)访问您想要的数据。这意味着在 JSON 对象(和 firebase 实时)中没有“加入”之类的东西。我建议阅读更多关于设计架构的最佳实践。是的,对于 firebase 实时,期望它有重复的数据(与 SQL dbs 不同)。
tl;博士:
- 将您的架构设计为尽可能平坦
不要将数据存储为数组。如果这样做,firebase 会将数组存储为一个对象,其键是数组的索引:
数组 = ['aha', 'hoho']
将存储为:
array: {
0: 'aha',
1: 'hoho'
}
- 设计您的架构,以便您可以在尽可能少的调用中获取所需的数据(是的,要做到这一点,期望重复数据):
例如:
{
users: {
user_id1: {
name: 'user 1',
phone: '123456',
email: 'testt@gmail.com'
}
},
events: {
participantst: {
user_id1: { // <= note for here I only want user's name & email thus its in this manner
name: 'user 1',
email: 'test@gmail.com'
}
}
}
}
不是
{
users: {
user_id1: {
name: 'user 1',
phone: '123456',
email: 'testt@gmail.com'
}
},
events: {
participantst: {
user_id1: user_id1 // <= Doing it like this means you would need to make another call to the 'users' node to fetch the user data
}
}
}
还有更多。
推荐阅读
- python - python无法打印日期时间信息
- node.js - 同时支持 Webpack、Parcel 和 Rollup 的最佳方法是什么?
- python - 无法在 Python 中导入“漂亮的”
- python - 有没有办法使用 Python 在视频的开头添加图像?
- sql-server - 使用 INSERT-SELECT 插入新记录时出现问题
- java - 将多个用户输入存储到多个单独的数组中
- c - 用无用的 printf 调用修复了条件返回值错误
- sql - 如果值已经存在,有没有办法从其他两列添加一个 int 的连接值来提供一个列?
- php - Docker 中的 PHP - fopen() 或 file_put_contents() 无法写入 /proc/1/fd/2 以进行标准输出写入
- android - 如何在 Android 中优化后台任务