【问题标题】:how to fetch data in firebase by user id [closed]如何通过用户ID在firebase中获取数据[关闭]
【发布时间】:2019-08-12 13:01:54
【问题描述】:

我将用户插入到 firebase 数据库中,每个用户都有自己的数据(电子邮件、姓名、年龄 ....),我尝试获取单个用户的数据 - 连接设备的用户 - 我的功能是带来数据,但是它带来了整个数据库,所有用户,你能帮忙解决这个问题吗? 这是我的代码:

componentWillMount(){
    firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        firebase.database().ref('friends')
            .on('value', (snapshot) => {
                const data = snapshot.val() || [];
                this.handleData(data);
            });
      }
   });
  }

【问题讨论】:

    标签: firebase react-native firebase-realtime-database firebase-authentication


    【解决方案1】:

    如果您还没有,则需要将有关每个用户的信息拆分为子集合。

    例如:朋友 > user_1 > 电子邮件、姓名、年龄......

    然后在您的 Firebase 查询中,您需要指定子集合。现在,您可以返回“朋友”下的每个条目。它应该看起来像这样:

    componentWillMount(){
        firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            firebase.database().ref('friends/' + user.uid) //reference uid of logged in user like so
                .on('value', (snapshot) => {
                    const data = snapshot.val() || [];
                    this.handleData(data);
                });
          }
       });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-20
      • 2020-04-04
      • 1970-01-01
      • 2021-06-08
      • 2022-11-29
      • 2017-05-30
      • 2017-10-19
      • 2020-04-02
      相关资源
      最近更新 更多