【问题标题】:Get only the current users data from firebase realtime database仅从 firebase 实时数据库中获取当前用户数据
【发布时间】:2018-08-03 14:12:48
【问题描述】:

我想从我的 firebase 实时数据库中获取数据,但我只想获取包含当前用户 UID 的数据。

数据库结构见图:

我没有从文档中了解 firebase 查询/排序的窍门:https://firebase.google.com/docs/database/web/lists-of-data

// Gets all chats
return firebase.database().ref(`chat`);

// would like to do something like this 
let userId = firebase.auth().currentUser.uid
firebase.database().ref('/chat/' + userId).once('value')

对于解决此问题的任何帮助或提示将不胜感激。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    没有办法直接根据 UID 查询聊天,因为键 end 以 UID 而不是以它们开头。这是预期的行为,如果您以您似乎正在尝试的方式解析密钥,我会认为这是一种代码异味。

    相反,保留每个用户的聊天 ID 列表。

    userChats
      uid1
        chatid1: true
        chatid2: true
      uid2
        chatid2: true
    

    这样,您可以从/users/$uid 加载用户的聊天 ID,然后加载每个单独的聊天。后者并不像一些开发人员认为的那么慢,因为 Firebase 通过单个连接对这些请求进行管道传输。见http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 2020-05-29
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 2021-03-04
      • 2021-03-05
      • 1970-01-01
      • 2019-10-22
      相关资源
      最近更新 更多