Ghislain Gndlf
Ghislain Gndlf

Reputation: 11

Search information by user on firebase

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,

Upvotes: 1

Views: 2843

Answers (1)

sbolel
sbolel

Reputation: 3526

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

  1. To check if a user exists by the identifiant property, you'd orderByChild("identifiant") and query for a specific user with the .equalTo("<identifient_here>").

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());
});
  1. If instead you want to query by the key (such as simplelogin:53), you could query by using orderByKey() instead of orderByChild()... or just simply setting the ref to the user's key like so:
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

  • 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.

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.

Upvotes: 1

Related Questions