How to get the key of a firebaseArray object

亡梦爱人 提交于 2019-12-11 05:44:22

问题


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


回答1:


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




回答2:


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!




回答3:


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



来源:https://stackoverflow.com/questions/38045352/how-to-get-the-key-of-a-firebasearray-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!