Question

I just set up my Firebase, angularFire database with my Yeoman, AngularJS app. I use to hard code my data before like so

$scope.data = [
    { name: 'NAME', description: 'DESCRIPTION', type: 'TYPE', etc: 'ETC', etc: 'ETC', etc: 'ETC'... },
    { name: 'NAME', description: 'DESCRIPTION', type: 'TYPE', etc: 'ETC', etc: 'ETC', etc: 'ETC'... },
    { name: 'NAME', description: 'DESCRIPTION', type: 'TYPE', etc: 'ETC', etc: 'ETC', etc: 'ETC'... },
    ...
    ...
    More Data...
    ...
    ...
    { name: 'NAME', description: 'DESCRIPTION', type: 'TYPE', etc: 'ETC', etc: 'ETC', etc: 'ETC'... }
]

when I uploaded the above data into my Firebase database I had to have a valid form of JSON data so I converted it to this

[
    { "name": "NAME", "description": "DESCRIPTION", "type": "TYPE", "ect": "ETC","ect": "ETC","ect": "ETC"... },
    { "name": "NAME", "description": "DESCRIPTION", "type": "TYPE", "ect": "ETC","ect": "ETC","ect": "ETC"... },
    { "name": "NAME", "description": "DESCRIPTION", "type": "TYPE", "ect": "ETC","ect": "ETC","ect": "ETC"... },
    ...
    ...
]

which is valid JSON format. Now, when I console.log(firebaseRef), my data is getting retrieved in the form below. This is exactly what I see in my Google Chrome Console.

Object {$bind: function, $add: function, $save: function, $set: function, $transaction: function…}
    0: Object
         description: "DESCRIPTION""
         name: "NAME"
         type: "TYPE""
         etc: "ETC"
         etc: "ETC"
         etc: "ETC"
         ...more data...
         __proto__: Object
    1: Object
         description: "DESCRIPTION""
         name: "NAME"
         type: "TYPE""
         etc: "ETC"
         etc: "ETC"
         etc: "ETC"
         ...more data...
         __proto__: Object
    2: Object
         description: "DESCRIPTION""
         name: "NAME"
         type: "TYPE""
         etc: "ETC"
         etc: "ETC"
         etc: "ETC"
         ...more data...
         __proto__: Object
    ...
    ...
    ...More Data...
    ...
    ...

How would I convert my new Object data I am retrieving from Firebase into the same form as I had before?

Here is a picture of the main difference between the two. The Firebase data I am receiving seems to be an Object with multiple Objects in it and the the data I want/used before is an array of multiple objects. picture of two different Object data

Was it helpful?

Solution

According to this url: Object to array - quick conversion

You need to assign the firebase object to a variable:

$scope.firebaseObj

and then you can stuff it into your $scope.data object by:

$scope.data = [];
 for (var i = 0; i < $scope.firebaseObj.length; i++) {
      $scope.data[i]['name']= $scope.firebaseObj[i].name;
      $scope.data[i]['description']= $scope.firebaseObj[i].description;
      $scope.data[i]['type'] =  $scope.firebaseObj[i].type;
      $scope.data[i]['etc1'] =  $scope.firebaseObj[i].etc1;
      $scope.data[i]['etc12'] =  $scope.firebaseObj[i].etc2;
      .
      .
      . 
 }

This only will work if every element in the JSON has the same attributes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top