首页 > 解决方案 > 使用 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);
      });
  });
};

这是我的 Firebase 数据库

标签: javascriptreact-nativeexporeact-native-firebase

解决方案


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


tl;博士:

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

    数组 = ['aha', 'hoho']

将存储为:

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

还有更多。


推荐阅读