【问题标题】:Join tables from firebase with React Native使用 React Native 连接来自 firebase 的表
【发布时间】:2019-06-08 08:36:43
【问题描述】:

嗨,我是 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);
      });
  });
};

This is my firebase database

【问题讨论】:

  • 2 件事。 1. 始终正确缩进代码以确保可读性。 2. 你用的是什么数据库? firebase 实时数据库还是 firestore?
  • firebase 实时,我读过我们在 firebase 中使用“.once”来连接两个表,但不知道如何使用它以及我在代码中附加的图片链接两个表中的部门名称和描述匹配,然后它们都被删除@Kiong

标签: javascript react-native expo react-native-firebase


【解决方案1】:

您不能像对待 RDBMS/SQL 数据库那样考虑 firebase realtime。将 firebase realtime 视为一个大型 JSON 对象,通过对象键(也是 URL)访问您想要的数据。这意味着在 JSON 对象(和 firebase 实时)中没有“加入”之类的东西。我建议阅读更多关于设计架构的最佳实践。是的,对于 firebase 实时,期望它有重复的数据(与 SQL dbs 不同)。


tl;博士:

  1. 将架构设计得尽可能扁平
  2. 不要将数据存储为数组。如果这样做,firebase 会将数组存储为一个对象,其键是数组的索引:

    数组 = [ ‘啊哈’, '嗬嗬' ]

将被存储为:

array: {
  0: 'aha',
  1: 'hoho'
}
  1. 设计您的架构,以便您可以在尽可能少的调用中获取您想要的数据(是的,要做到这一点,预计会有重复的数据):

例如:

{
  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
    }
  }
}

还有更多。

【讨论】:

  • 谢谢你,这肯定会增加我的知识,现在你能告诉我我应该在我的代码中做什么,以便如果两个字段名称在部门和社团表中匹配,那么如果我从部门中删除值,然后从社会中删除相同的值。
  • 2 个选择:1. 手动触发两个节点上的删除。 2.为firebase创建一个云函数来监听department节点上的删除,然后从society节点中删除相同的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 2018-07-14
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多