2015-12-29 36 views
0

我正在使用Angular將數據(用戶及其訂單)添加到Firebase。從角度檢索和呈現Firebase中的數據

以下是Firebase中的數據庫結構。

Firebase DB Structure

下面是我使用的檢索從火力地堡特定用戶的所有訂單的代碼。

var orddataref = fbref.child("profile").child($scope.cart.userkey).child("orders"); 
orddataref.on("child_added", function(snap) { 
    ordersRef.child(snap.key()).once("value", function(snapshot) { 
     $scope.odata = snapshot.val(); 
     $scope.apply(); 
    }); 
}); 

以下是我在HTML中使用RENDER的代碼。

<div ng-repeat="order in odata"> 
     <div> Order Address : {{order.address}}</div> 
     <div> Deliverynotes : {{order.deliverynotes}}</div> 
     <div> Total Amount : {{order.totalamount}}</div> 
      <ion-item class ="item" ng-repeat="item in order.items"> 
       <div> Item SKU : {{item.sku}}</div> 
       <div> Price : {{item.price}}</div> 
       <div> Quantity : {{item.quantity}}</div> 
      </ion-item> 
</div> 

它只返回當前項目而不是所有訂單(包括以前的訂單)。 任何人都可以建議我去哪裏錯了嗎?任何幫助非常感謝。

編輯後弗蘭克的建議 弗蘭克,我把一個斷點就行了$scope.odata = snapshot.val();它只是具有下述目前的訂單。

{"address":"Auckland, New Zealand","deliverynotes":"At door","items":[{"price":3,"quantity":2,"sku":"1002"},{"price":6.5,"quantity":3,"sku":"2002"}],"totalamount":25.5} 

所以我把它放在一個記錄器。 console.log(「displayOrders - orderdata:===」,$scope.odata); console.log(「displayOrders - orderdata:===」,$scope.odata);

下面是我看到所有訂單的Chrome控制檯的屏幕抓圖。

All Orders

我如何獲得這些訂單爲$scope.odata

+0

你已經調試過了嗎?在'$ scope.odata = snapshot.val();'這一行上放置一個斷點,看看觸發時會發生什麼。 'snapshot.val()'可能與你期望的非常不同。 –

回答

0

只是破解它。 以下是獲取特定用戶所有訂單的正確代碼。 如果需要,請參考問題中的數據庫結構。

$scope.odata = {}; 
    $scope.tempdata =[]; 
    var fbref = new Firebase("https://xxxxxxx.firebaseio.com/"); 
    var ordersRef = fbref.child("orders"); 
    var orddataref = fbref.child("profile").child($scope.cart.userkey).child("orders"); 
     orddataref.on("child_added", function(snap) { 
      ordersRef.child(snap.key()).once("value", function(snapshot){ 
       $scope.tempdata.push(JSON.parse(JSON.stringify(snapshot.val()))); 
      }); 
     }); 
$scope.odata = $scope.tempdata; 

由於我是新手,如果有人有更好的解決方案,請建議。 @弗蘭克,謝謝你指出正確的道路。