使用Firebase按名称获取用户 [英] Get users by name property using Firebase

查看:186
本文介绍了使用Firebase按名称获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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

我环顾四周,找不到任何东西访问个人数据,更不用说当他们被给予一些随机哈希作为他们的ID。

我将如何去抓住基于他们的名字的个人用户信息?如果有更好的方法,请告诉我!

解决方案之前,Firebase要求您生成自己的索引下载某个位置的所有数据以查找和检索与某个子属性相匹配的元素(例如,所有使用 name ===Alex的用户) 。
$ b

2014年10月,Firebase通过 orderByChild()方法推出了新的查询功能,快速高效地进行这种查询。请参阅下面的更新回答。




向Firebase写入数据时,有几个不同的选项可以反映不同的用例。在高层次上,Firebase是一个树形结构的NoSQL数据存储,并提供了一些用于管理数据列表的简单基元:


  1. <使用一个唯一的已知键写入到Firebase:

      ref.child('users' ).child('123').set({first_name:rob,age:28})

  2. $


  3. 附加到列表中,并自动生成一个按键, > ref.child('users')。push({first_name:rob,age:28})

    <$ p

  4. $($'$'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'通过 key 过滤或数据到列表中 / em>或属性值

      //获取最后10个用户,按键排序
    ref.child('users')。orderByKey()获取所有年龄大于等于25的用户
    ref.child('users')。orderByChild(()){(child_added',...)
    $ b $ 'age')。startAt(25).on('child_added',...)


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

  ref.child('users')。orderByChild(' ('child_added',...)






这里是Firebase的工程师。将数据写入Firebase时,您有几个不同的选项可以反映不同的应用程序使用情况。由于Firebase是NoSQL数据存储区,因此您需要使用唯一键存储数据对象,以便您可以直接访问该项目,或者在特定位置加载所有数据,并遍历每个项目以查找您正在查找的节点对于。请参阅编写数据

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

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

$ $ $ $ $ p $函数escapeEmailAddress(email){
if(!email)return false

//将'。'(不允许在Firebase密钥中)替换为','(不允许在电子邮件地址中)
email = email.toLowerCase();
email = email.replace(/\./g/,',');
返回电子邮件;
}

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不允许在引用中使用某些字符(请参阅创建引用),我们删除,并用<$ c $替换它c>,


I'm trying to create an application where I can get/set data in specific users accounts and I was tempted by Firebase.

The problem I'm having is that I don't know how to target specific users data when my structure looks like this:

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

I've looked around and I can't really find anything on how to access individual data let alone when they're given some random hash as their ID.

How would I go about grabbing individual user information based of their name? If there is a better way of doing this please tell me!

解决方案

Previously, Firebase required you to generate your own indexes or download all data at a location to find and retrieve elements that matched some child attribute (for example, all users with name === "Alex").

In October 2014, Firebase rolled out new querying functionality via the orderByChild() method, that enables you to do this type of query quickly and efficiently. See the updated answer below.


When writing data to Firebase, you have a few different options which will reflect different use cases. At a high level, Firebase is a tree-structured NoSQL data store, and provides a few simple primitives for managing lists of data:

  1. Write to Firebase with a unique, known key:

    ref.child('users').child('123').set({ "first_name": "rob", "age": 28 })
    

  2. Append to lists with an auto-generated key that will automatically sort by time written:

    ref.child('users').push({ "first_name": "rob", "age": 28 })
    

  3. Listen for changes in data by its unique, known path:

    ref.child('users').child('123').on('value', function(snapshot) { ... })
    

  4. Filter or order data in a list by key or attribute value:

    // 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', ...)
    

With the addition of orderByChild(), you no longer need to create your own index for queries on child attributes! For example, to retrieve all users with the name "Alex":

ref.child('users').orderByChild('name').equalTo('Alex').on('child_added',  ...)


Engineer at Firebase here. When writing data into Firebase, you have a few different options which will reflect different application use cases. Since Firebase is a NoSQL data store, you will need to either store your data objects with unique keys so that you can directly access that item, or load all data at a particular location and loop through each item to find the node you're looking for. See Writing Data and Managing Lists for more information.

When you write data in Firebase, you can either set data using a unique, defined path (i.e. a/b/c), or push data into a list, which will generate a unique id (i.e. a/b/<unique-id>) and allow you to sort and query the items in that list by time. The unique id that you're seeing above is generated by calling push to append an item to the list at online-b-cards/users.

Rather than using push here, I would recommend using set, and storing the data for each user using a unique key, such as the user's email address. Then you can access the user's data directly by navigating to online-b-cards/users/<email> via the Firebase JS SDK. For example:

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 });

Note that since Firebase does not permit certain characters in references (see Creating References), we remove the . and replace it with a , in the code above.

这篇关于使用Firebase按名称获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆