2016-11-23 48 views
0

我在服務器端構建了一個RESTful應用程序運行節點,如express和firebase-admin。我製作了一些API端點與我的Firebase項目數據庫鏈接。如何使用Angular資源和firebase-admin檢索Firebase密鑰?

在客戶端,我正在使用Angular資源來使用API​​。下面是我的路線文件,其中解決了名爲「BeerService」資源服務的一部分:

.state('beers', { 
        url: '/beers', 
        templateUrl: 'app/beer/beer.html', 
        controller: 'BeerCtrl', 
        controllerAs: 'vm', 
        resolve: { 
         beers: function(BeerService) { 
          return BeerService.query(function(data) { 
           console.info(data); 
           return data; 
          }, function(error) { 
           console.error(error); 
           return error; 
          }); 
         } 
        } 
       }) 

它返回data並將其保存到beers。然後它被注入我的控制器並保存在一個名爲vm.beers的全局變量中。在html上有一個ng-repeat(vm.beers中的啤酒)和{{beer}}。

問題:啤酒只顯示來自對象的值。我如何獲得這些節點的密鑰?

這是console.info(data)從文件上面的輸出(有一個在DB只是一個記錄與關鍵-KXHTyo8AWFehyJ2iIY_):

console output

這是HTML:

<h5>GET /api/beers</h5> 
<button ng-click="vm.queryBeers()" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent"> 
    Refresh 
</button> 
<ul class="demo-list-item mdl-list"> 
    <li class="mdl-list__item" ng-repeat="beer in vm.beers"> 
     <span class="mdl-list__item-primary-content"> 
      {{beer}} 
     </span> 
    </li> 
    {{vm.beers}} 
</ul> 

這顯示爲:

screen

你能幫助我知道如何從數據中獲得密鑰(-KXHTyo8AWFehyJ2iIY_)嗎?

+0

很好,沒有人回答如何到達這裏使用火力methods..so節點關鍵是一種選擇,設定密鑰的兒童在使用push方法像下面創建: 'saveBeer:功能(REQ ,res,next){ var ref = beersRef.push(); var key = ref.key; ref.set({ 鍵:鍵, 味精: '喝我', 水平:9000 },功能(錯誤){ 如果(錯誤){ res.sendStatus(400); }其他{ (200) } }); }' – gcfabri

回答

0

我在文檔中找到了解決方案。這裏是:

// Assume we have the following data in our database: 
{ 
    "users": { 
    "ada": { 
     "first": "Ada", 
     "last": "Lovelace" 
    }, 
    "alan": { 
     "first": "Alan", 
     "last": "Turing" 
    } 
    } 
} 

// Loop through users in order with the forEach() method. The callback provided 
// to will be called synchronously with a DataSnapshot for each child: 
var query = firebase.database().ref("users").orderByKey(); 
query.once("value") 
    .then(function(snapshot) { 
    snapshot.forEach(function(childSnapshot) { 
     // key will be "ada" the first time and "alan" the second time 
     var key = childSnapshot.key; 
     // childData will be the actual contents of the child 
     var childData = childSnapshot.val(); 
    }); 
});