Created
June 20, 2015 09:32
-
-
Save slashthinking/8452dd416bbe9c1786d0 to your computer and use it in GitHub Desktop.
通过邮箱名搜索用户
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
* Assuming you can't use priorities (e.g. they are already being used for something else) | |
* You can store the email addresses in an index and use that to match them to user ids | |
***************************************************/ | |
var fb = new Firebase(URL); | |
/** | |
* Looks up a user id by email address and invokes callback with the id or null if not found | |
* @return {Object|null} the object contains the key/value hash for one user | |
*/ | |
function getUserIdByEmail( emailAddress, callback ) { | |
fb.child('emails_to_ids/'+emailToKey(emailAddress)).once('value', function(snap) { | |
callback( snap.val() ); | |
}); | |
} | |
/** | |
* Creates a new user record and also updates the index | |
*/ | |
function createNewUser( userRecord ) { | |
var id = fb.child('user').push(userRecord).name(); | |
fb.child('emails_to_ids/'+userRecord.email).set(id); | |
return id; | |
} | |
/** | |
* Firebase keys cannot have a period (.) in them, so this converts the emails to valid keys | |
*/ | |
function emailToKey(emailAddress) { | |
return emailAddress.replace('.', ','); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
* You can set the priority on the user records to the email address, then use that to look them up. | |
* Assumes user records are stored in the /user path | |
***************************************************/ | |
var fb = new Firebase(URL); | |
/** | |
* @param {string} emailAddress | |
* @return {Object} the object contains zero or more user records, the keys are the users' ids | |
*/ | |
function findUsersMatchingEmail( emailAddress, callback ) { | |
fb.child('user').startAt(emailAddress).endAt(emailAddress).once('value', function(snap) { | |
// the keys are the user ids, the values are objects containing each user record that matched (presumably 1?) | |
callback( snap.val() || {} ); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment