Get users by name property using Firebase -
this noob question here goes... i'm trying create application can get/set info in specific users accounts , tempted firebase.
the problem i'm having don't know how target specific users info when construction looks this:
i've looked around , can't find on how access individual info allow lone when they're given random hash id.
how go grabbing individual user info based of name? if there improve way of doing please tell me!
cheers - richard
previously, firebase required generate own indexes or download info @ location find , retrieve elements matched kid attribute (for example, users name === "alex"
).
in oct 2014, firebase rolled out new querying functionality via orderbychild()
method, enables type of query , efficiently. see updated reply below.
when writing info firebase, have few different options reflect different utilize cases. @ high level, firebase tree-structured nosql info store, , provides few simple primitives managing lists of data:
write firebase unique, known key:
ref.child('users').child('123').set({ "first_name": "rob", "age": 28 })
append lists auto-generated key automatically sort time written:
ref.child('users').push({ "first_name": "rob", "age": 28 })
listen changes in info unique, known path:
ref.child('users').child('123').on('value', function(snapshot) { ... })
filter or order info in list key or attribute value:
// lastly 10 users, ordered key ref.child('users').orderbykey().limittolast(10).on('child_added', ...) // users age >= 25 ref.child('users').orderbychild('age').startat(25).on('child_added', ...)
with add-on of orderbychild()
, no longer need create own index queries on kid attributes! example, retrieve users name "alex":
ref.child('users').orderbychild('name').equalto('alex').on('child_added', ...)
engineer @ firebase here. when writing info firebase, have few different options reflect different application utilize cases. since firebase nosql info store, need either store info objects unique keys can straight access item, or load info @ particular location , loop through each item find node you're looking for. see writing data , managing lists more information.
when write info in firebase, can either set
info using unique, defined path (i.e. a/b/c
), or push
info list, generate unique id (i.e. a/b/<unique-id>
) , allow sort , query items in list time. unique id you're seeing above generated calling push
append item list @ online-b-cards/users
.
rather using push
here, recommend using set
, , storing info each user using unique key, such user's email address. can access user's info straight navigating online-b-cards/users/<email>
via firebase js sdk. example:
function escapeemailaddress(email) { if (!email) homecoming false // replace '.' (not allowed in firebase key) ',' (not allowed in email address) email = email.tolowercase(); email = email.replace(/\./g, ','); homecoming 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 since firebase not permit characters in references (see creating references), remove .
, replace ,
in code above.
firebase
No comments:
Post a Comment