用户在Firebase上搜索信息 [英] Search information by user on firebase
问题描述
我试图创建一个应用程序,我可以创建用户和存储Firebase的信息。这里有一个例子:
http://image.noelshack.com/fichiers/2015/11/1426182307-log.png
但现在我想要如果有人想创建一个新用户,请检查名称是否已经存在。
如何在不知道id的情况下抓取个人用户信息,如simplelogin:54? / p>
我找到了这个主题通过名称属性使用Firebase获取用户但它不是一回事,因为在我的情况下,我不知道用户后的孩子
干杯, 像弗兰克说过的,你必须知道一些关于用户能够看到她/ p>
然而,这里是一个普遍的答案。我假设名称是指您创建的已识别属性。
建议
- 首先查看 Firebase查询文档
短回答
- 要通过
identifiant
属性检查用户是否存在,您需要orderByChild(identifiant)
并使用.equalTo(< identifient_here>)
查询特定的用户。
例如,要检查使用identifyient =aaa,
var identifient =aaa;等等(识别).once(价值,功能(快照){
console.log(加载的用户由识别:,识别,snapshot.val)。
usersRef.orderByChild ());
});
simplelogin:53
),您可以使用 orderByKey()
而不是 orderByChild()
...或者只是简单地将ref设置为用户的键:
var userKey ='simplelogin:53';
。
var userRef = new Firebase(https://< YOUR-FIREBASE-APP> .firebaseio.com / Users+ userKey);
userRef.once(value,function(snapshot){
console.log(Loaded user,snapshot.val());
});
$ b $ h $ Long(er)Answer
- 您可以使用用户工厂来处理这个问题(请参阅 Angular Providers文档)。
- 使用
li>$ q
服务返回工厂中的承诺。
- 以下是<$ c $的 Angular API文档c> $ q
UserFactory示例
- 查看此工作 PLNKR示例
- 它与我的一个公共Firebase实例绑定。
- 我创建了相同的
simplelogin:53 $> c $ c $>用户 / Users
就像你有。
- 如果您搜索
identifient code> =
aaa
,您将得到正确的用户。 - 控制器i这里的补充仅仅是为了举例的目的,并没有真正做任何有价值的事情。
- Start by looking over the Firebase Query documentation.
- To check if a user exists by the
identifiant
property, you'dorderByChild("identifiant")
and query for a specific user with the.equalTo("<identifient_here>")
. - If instead you want to query by the key (such as
simplelogin:53
), you could query by usingorderByKey()
instead oforderByChild()
... or just simply setting the ref to the user's key like so: - You can handle this with a user factory (see Angular Providers documentation).
- You return a promise in the factory using the
$q
service. - Here is the Angular API documentation for
$q
. - Check out this working PLNKR example.
- It's tied to one of my public Firebase instances.
- I created the same
simplelogin:53
user in/Users
like you have. - If you search for the
identifient
=aaa
, you'll get the right user. - The controller implementation here is for example purposes, and doesn't really do anything worth while. It's just for reference.
$ {
Users:{
simplelogin:53:{
identifiant:aaa
}
}
$ / code $ / pre
$ b
UserFactory
.factory('UserFactory',function($ q,$ firebaseObject,fbUrl){
return function(userId){
var deferred = $ q.defer();
if(userId.isNotEmpty()){
var userRef = new Firebase(fbUrl +'/Users/').orderByChild(\"identifiant\" ).equalTo(userId );
userRef.once(value,
function(dataSnapshot){
if(dataSnapshot.val()){
console.log(Loaded user,dataSnapshot .val());
deferred.resolve(dataSnapshot.val());
} else {
console.info(通过id找不到用户,userId);
deferred.reject(找不到用户识别符='+ userId + );
}
},
功能(误差){
console.error( 错误加载用户,错误);
deferred.reject(error);
}
);
} else {
deferred.reject(No id entered!);
}
return deferred.promise;
$ h $ $
.controller('HomeController',function($ scope,UserFactory){
$ scope.identifient =''; $
showUser(false);
$ scope.error = errorMessage;
} else {
删除$ scope.error;
var showUser = function(userObject){
if(userObject){
showError(false);
$ scope.user = userObject;
} else {
delete $ scope.user;
}
}
$ scope.loadUser = function(){
userPromise = new UserFactory($ scope.identifient);
userPromise.then(function(data){
showUser(data);
})。catch(function(error){
showError(error);
});
}
})
模板
< div ng-controller =HomeController>
< h2>主页模板< / h2>
< input ng-model =identifientplaceholder =identifient/>
< button ng-click =loadUser()>查找用户< / button>
< hr />
< div ng-if =user>用户:{{user}}< / div>
< div ng-if =error>错误:{{error}}< / div>
< / div>
希望有帮助。
This is probably very simple ,so sorry...
I'm trying to create an application where i can create users and store informations by Firebase. There is an example here :
http://image.noelshack.com/fichiers/2015/11/1426182307-log.png
But now I want to check if the name already exists if someone wants to create a new user.
How would I go about grabbing individual user information without knowing their id , like simplelogin:54?
I've found this topic Get users by name property using Firebase but it is not the same thing because in my case I don't know the children after "Users"
Cheers,
解决方案 Like Frank said, you must know something about the user to be able to look her/him up.
However, here is a general answer. I am assuming that by "name" you mean the property "identifiant" that you've created.
Suggestions
Short Answer
For example, to check if a user with identifient="aaa",
var usersRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/Users");
var identifient = "aaa";
usersRef.orderByChild("identifiant").equalTo(identifient).once("value", function(snapshot) {
console.log("Loaded user by identifient:",identifient,snapshot.val());
});
var userKey = 'simplelogin:53';
var userRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/Users" + userKey);
userRef.once("value", function(snapshot) {
console.log("Loaded user",snapshot.val());
});
Long(er) Answer
Example with UserFactory
The Data
{
"Users" : {
"simplelogin:53" : {
"identifiant" : "aaa"
}
}
}
UserFactory
.factory('UserFactory', function($q, $firebaseObject, fbUrl){
return function(userId){
var deferred = $q.defer();
if (userId.isNotEmpty()) {
var userRef = new Firebase(fbUrl + '/Users/').orderByChild("identifiant").equalTo(userId);
userRef.once("value",
function(dataSnapshot){
if (dataSnapshot.val()) {
console.log("Loaded user",dataSnapshot.val());
deferred.resolve(dataSnapshot.val());
} else {
console.info("Couldn't find user by id",userId);
deferred.reject("No user found by identifient = '"+userId+"'");
}
},
function(error){
console.error("Error loading user",error);
deferred.reject(error);
}
);
} else {
deferred.reject("No id entered!");
}
return deferred.promise;
}
})
Controller
.controller('HomeController',function($scope, UserFactory) {
$scope.identifient = '';
var showError = function(errorMessage) {
if (errorMessage) {
showUser(false);
$scope.error = errorMessage;
} else {
delete $scope.error;
}
}
var showUser = function (userObject) {
if (userObject) {
showError(false);
$scope.user = userObject;
} else {
delete $scope.user;
}
}
$scope.loadUser = function() {
var userPromise = new UserFactory($scope.identifient);
userPromise.then(function(data){
showUser(data);
}).catch(function(error){
showError(error);
});
}
})
Template
<div ng-controller="HomeController">
<h2>Home Template</h2>
<input ng-model="identifient" placeholder="identifient"/>
<button ng-click="loadUser()">Find User</button>
<hr/>
<div ng-if="user">User: {{user}}</div>
<div ng-if="error">Error: {{error}}</div>
</div>
Hope that helps.
这篇关于用户在Firebase上搜索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!