Jojo Bebert
Jojo Bebert

Reputation: 33

Firebase, getting an object after a search

Hello My goal is to test if an employee exists with the $scope.CodeBarre value, into my FIREBASE database, then, the program will open a door, if $CodeBarre exists. The $CodeBarre var comes from a USB barcode Scan, that will scan the employe CardId...

This is my actual code :

  /* TEST DU CODE BARRE EN LIGNE */
 $scope.CodeBarre="GA13081982";
function testerLeCodebarre() {

    var ref = new Firebase('https://blinding-heat-8502.firebaseio.com/employes').orderByChild("identifiant").equalTo($scope.CodeBarre);
    $scope.employe = $firebaseArray(ref);

    console.log($scope.employe);
} 

I'm getting the object in an array who $scope.employe that looks like this :

picture of the result into firebug

I'm getting well the right employee but it's impossible to get it working into the angularJs view after, i simply can't get the name of the employe for exemple by typing $scope.employe[0].nom

Note : if the employee is not found, then the returned array by firebase is not empty, but i simply can't find the employee object....

I've also tried with $firebaseObject but i can't get a regular AngularJs object .

Please could you help thank you

This is how my template looks like :

<div class="row">
        Bienvenue, {{employe.prenom}} {{employe.nom}}
</div>
  1. Trying with $firebaseObject:

When i type this, trying to get an object instead of an array, it finds the employee well, but the problem is I can't access to it into my template, because I don't know the $ID by advance(this $ID is generated by Firebase) :

/* TEST DU CODE BARRE EN LIGNE */

function testerLeCodebarre() {

var ref = new Firebase('https://blinding-heat-8502.firebaseio.com/employes').orderByChild("identifiant").equalTo($scope.CodeBarre);
$scope.employe = $firebaseObject(ref);
console.log($scope.employe);

}

This is what i get into Firebug :

[Firebug when getting one object][2]

My wish would be simply to populate $scope.employe with the object who's got the $id -KIcVJh9sdVSWMamGaA3 that i'm getting from firebase, but i really don't know how to do it.

EDIT: So it works well with this :

    var ref = new Firebase('https://blinding-heat-8502.firebaseio.com/employes').orderByChild("identifiant").equalTo($scope.CodeBarre).once("value", function(snapshot) {
   var data = snapshot.val(); 
   if (data){
       console.log(data);

     //employee exists
     $scope.employe = data;
     console.log($scope.employe);
   }

But $scope.employe = data doesn't work in my template :

<div class="row">
        Bienvenue, {{employe.prenom}} {{employe.nom}}
</div>

I still can't access the object, plus considering that i dont know the $id by advance :

Object returned by firebase

I've tried to type in my controller data.nom or data.age but gets undefined. I've also tried $scope.employe = data.nom; but get undefined

Upvotes: 2

Views: 139

Answers (1)

adolfosrs
adolfosrs

Reputation: 9389

As I can see you will be always retrieving one employee so there is no need for using $firebaseArray in your case. Simply go with:

var ref = new Firebase('https://blinding-heat-8502.firebaseio.com/employes').orderByChild("identifiant").equalTo($scope.CodeBarre).on("child_added", function(snapshot) {
   var data = snapshot.val(); 
   if (data){
     //employee exists
     $scope.employe = data;
   }
});

Upvotes: 1

Related Questions