MacD MacD - 9 months ago 45
AngularJS Question

Return value from firebase database Javascript

My attempts at retrieving the value of a firebase record are failing. My JSON is as follows:

{
"accounts" : {
"-KRPSyO4B48IpBnHBTYg" : {
"dateCreated" : "",
"email" : "",
"provider" : "",
"userId" : ""
}
},
"products" : {
"-KUKRafaNurpFhGF4jQa" : {
"name" : ""

}
},
"total" : 1
}


I'm trying to return the value of "total". Here's the latest attampt:

var totals = firebase.database().ref('total').once('value', function(products) {});
$scope.totalPosts = totals;


and my HTML:

<div class="card" ng-controller="myController">{{totalPosts}}</div>


All I'm getting is an empty {}. What am I doing wrong?

Thanks in advance!

Answer Source

Firebase queries are asynchronous, try this.

firebase.database().ref('total').once('value', function(products) {
  $scope.totalPosts = products.val();
});

Read and write Data