简体   繁体   English

Firebase 从父级获取基于key的数据

[英]Firebase get data based on key from parent

I got a parent table which is users and it has 3 childs in it.我有一个父表,它是users ,里面有 3 个孩子。 Can I access a child's data by just passing an uid?我可以通过传递一个 uid 来访问孩子的数据吗? Here is my user structure这是我的用户结构

在此处输入图像描述

What I tried is我试过的是

    firebase.database().ref().child('users').equalTo(localStorage.getItem('uid')).once('value',snap =>{
      console.log(snap.val())
    })

I get an null value everytime.我每次都得到一个null值。 I just want to grab what type of user when logging in so that I can display the appropriate UI for every user.我只是想在登录时抓取什么类型的用户,以便我可以为每个用户显示适当的 UI。 Im just new to firebase.我刚接触 firebase。

Your database structure does not allow the kind of query you are trying to perform.您的数据库结构不允许您尝试执行的查询类型。 You can only filter child nodes at one level deep from root node of your query.您只能在查询的根节点深处的一层过滤子节点。 The root node here is "users", but your actual user data is two levels deep under it.这里的根节点是“用户”,但你的实际用户数据在它下面有两层。

If you want to find the type of user given only a UID, you will need a structure that puts all users under the same root node:如果你想找到只给定 UID 的用户类型,你需要一个将所有用户放在同一个根节点下的结构:

- users
   - {uid}
       - type: "doctors" | "patients" | "secretary"
       - email: ...
       - name: ...

Now you can very easily query this for a UID to find out what type it is现在你可以很容易地查询这个 UID 来找出它是什么类型

firebase.database().ref().child('users').child(uid).once('value', ...)

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

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