Sean Cook
Sean Cook

Reputation: 435

How to get the key of a firebaseArray object

I am trying to get the key for a returned firebaseArray record but am having no luck.

I have aservice that runs the following to return a store

getStore: function(uid, id){
    var ref = firebase.database().ref('u/'+uid+'/stores/'+id+'/i/');
     // to take an action after the data loads, use the $loaded() promise
     return $firebaseArray(ref);
}

I use the following to get eeach record by calling the service

for(var i = 0; i < $scope.storesList.length; i++){
    var storeInfo = Stores.getStore($rootScope.user['uid'], $scope.storesList[i]['$id']);
    storeInfo.$loaded().then(function(data){
            $scope.stores.push(data);
            console.log($scope.stores);
    });
}

In my html I can access various properties within the array

<li ng-repeat="store in stores">
      <span class="store-address">{{store.$getRecord('name').$value}}</span>
      <span class="store-address">{{store.$getRecord('address1').$value}}</span>
</li>

But I cannot seem to get the id for each store record from within the ng-repeat

Upvotes: 1

Views: 2891

Answers (3)

Sean Cook
Sean Cook

Reputation: 435

I solved this by switch from firebaseArray to firebaseObject which contains an $id parameter

Upvotes: 0

utxin
utxin

Reputation: 64

To get the id simply call the $id on the item.

<li ng-repeat="store in stores">
      <span class="store-address">{{store.$getRecord('name').$value}}</span>
      <span class="store-address">{{store.$getRecord('address1').$value}}</span>
      <span class="store-address">{{store.$id}}</span>
</li>

See: https://github.com/firebase/angularfire/blob/master/docs/reference.md#firebasearray

Upvotes: 1

gsthina
gsthina

Reputation: 1100

Instead of getting yourself complicated, try this simple code!

var ref = new Firebase(url);
ref.once("value", function(list) {
    var subData    = list.val();
    var subDataKey = list.key();
    console.log(subDataKey);

This must help you!

Upvotes: 0

Related Questions