简体   繁体   English

从Firebase实时数据库中仅获取当前用户数据

[英]Get only the current users data from firebase realtime database

I would like to get data from my firebase realtime database, but I only want to get the data that includes the current users UID. 我想从Firebase实时数据库中获取数据,但是我只想获取包含当前用户UID的数据。

See the image for the database structure: 有关数据库结构,请参见图像:

db的图片,说明了问题

I don't get the hang of firebase querying/sorting from the docs: https://firebase.google.com/docs/database/web/lists-of-data 我没有从文档中获取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')

Would appreciate any help or tips for solving this. 将不胜感激任何帮助或提示,以解决此问题。

There is no way to directly query the chats based on the UID, since the keys end with the UID and don't start with them. 由于键以UID 结尾并且不是以UID开头,因此无法直接基于UID查询聊天。 This is expected behavior, I would consider it a code smell if you parsed keys in the way you seem to be trying. 这是预期的行为,如果您以尝试的方式解析键,我会认为这是一种代码味道。

Instead, keep a list of the chat IDs for each user. 而是保留每个用户的聊天ID列表。

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

This way you can load the chat IDs for a user from /users/$uid and then load each individual chat. 这样,您可以从/users/$uid加载用户的聊天ID,然后加载每个单独的聊天。 The latter is not nearly as slow as some developers think it is, because Firebase pipelines these requests over a single connection. 后者的速度不像某些开发人员所想的那样慢,因为Firebase通过单个连接流水线处理这些请求。 See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 参见http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM