firebase - 使用 Firebase 按名称属性获取用户

标签 firebase firebase-realtime-database

我正在尝试创建一个可以在特定用户帐户中获取/设置数据的应用程序,Firebase 很吸引我。

我遇到的问题是,当我的结构如下所示时,我不知道如何定位特定用户数据:

My data structure described by the text below.

online-b-cards
  - users
    - InnROTBVv6FznK81k3m
       - email: "hello@hello"
       - main:  "Hello world this is a text"
       - name:  "Alex"
       - phone: 12912912

我环顾四周,并没有真正找到任何关于如何访问个人数据的信息,更不用说当他们被给予一些随机哈希值作为 ID 时。

我将如何根据用户的姓名获取个人用户信息?如果有更好的方法请告诉我!

最佳答案

以前,Firebase 要求您生成自己的索引下载某个位置的所有数据,以查找和检索与某些子属性匹配的元素(例如,具有 name === "Alex" 的所有用户)。

2014 年 10 月,Firebase 通过 orderByChild() 推出了新的查询功能方法,使您能够快速有效地执行此类查询。请参阅下面更新的答案。

<小时/>

将数据写入 Firebase 时,您有几个不同的选项来反射(reflect)不同的用例。从较高层次来看,Firebase 是一个树形结构的 NoSQL 数据存储,并提供了一些用于管理数据列表的简单原语:

  1. 使用唯一的已知 key 写入 Firebase:

    ref.child('users').child('123').set({ "first_name": "rob", "age": 28 })
    
  2. 附加到带有自动生成键的列表,该键将按写入时间自动排序:

    ref.child('users').push({ "first_name": "rob", "age": 28 })
    
  3. 通过其唯一的已知路径监听数据变化:

    ref.child('users').child('123').on('value', function(snapshot) { ... })
    
  4. 属性值过滤列表中的数据或按排序列表中的数据:

    // Get the last 10 users, ordered by key
    ref.child('users').orderByKey().limitToLast(10).on('child_added', ...)
    
    // Get all users whose age is >= 25
    ref.child('users').orderByChild('age').startAt(25).on('child_added', ...)
    

添加 orderByChild() ,您不再需要为子属性的查询创建自己的索引!例如,要检索名为“Alex”的所有用户:

ref.child('users').orderByChild('name').equalTo('Alex').on('child_added',  ...)
<小时/>

这里是 Firebase 的工程师。将数据写入 Firebase 时,您有一些不同的选项,这些选项将反射(reflect)不同的应用程序用例。由于 Firebase 是 NoSQL 数据存储,因此您需要使用唯一键存储数据对象,以便可以直接访问该项目,或者加载特定位置的所有数据并循环遍历每个项目以找到您要查找的节点。请参阅Writing DataManaging Lists了解更多信息。

当您在 Firebase 中写入数据时,您可以 set使用唯一的定义路径(即 a/b/c )或 push 的数据将数据放入列表中,这将生成一个唯一的 id(即 a/b/<unique-id> )并允许您按时间排序和查询该列表中的项目。您在上面看到的唯一 ID 是通过调用 push 生成的将项目追加到 online-b-cards/users 的列表中.

而不是使用push在这里,我建议使用 set ,并使用唯一 key (例如用户的电子邮件地址)存储每个用户的数据。然后您可以通过导航到 online-b-cards/users/<email> 直接访问用户的数据。通过 Firebase JS SDK。例如:

function escapeEmailAddress(email) {
  if (!email) return false

  // Replace '.' (not allowed in a Firebase key) with ',' (not allowed in an email address)
  email = email.toLowerCase();
  email = email.replace(/\./g, ',');
  return email;
}

var usersRef = new Firebase('https://online-b-cards.firebaseio.com/users');
var myUser = usersRef.child(escapeEmailAddress('hello@hello.com')) 
myUser.set({ email: 'hello@hello.com', name: 'Alex', phone: 12912912 });

请注意,由于 Firebase 不允许在引用中使用某些字符(请参阅 Creating References ),因此我们删除了 .并将其替换为 ,在上面的代码中。

关于firebase - 使用 Firebase 按名称属性获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14963776/

相关文章:

java - 带推送 utf8 字符串的 Firebase Java API

javascript - 添加具有自动递增的自定义 ID 的子项 - Firebase

javascript - Firebase - 在 localStorage 对象有效时保持用户登录

javascript - Firebase 电子邮件验证工作流程

java - Android Firebase 查询多个值

android - 在服务中获取实时数据库引用时出错

ios - 在 UITableView 数组中保存 Firebase 结构 [Swift 4]

javascript - 你如何从 firestore 读取数据?

android - Gradle 同步失败 - com.google.android.gms :play-services-basement is being requested by various other libraries

ios - 使用 XLPagerTabStrip 在选项卡之间共享数据