Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am working on an ionic app that loads data from firebase. In this case I have a .factory called NoteStore that reads a specific table in firebase (eg "lakes").

{
  "lake": {   
     "one": {
        "bio": "xxxx",
        "shortname": "xxxx",
        "name": "xxxxx",
        "reknown": "xxxx",
        "image":"xxxxx"
    },
    "two": {
        "bio": "xxxx",
        "shortname": "xxxx",
        "name": "xxxxx",
        "reknown": "xxxx",
        "image":"xxxxx"
    }

  } 
} 

This is the .factory code that reads from firebase:

 //get all lake activites
 getAllLakes: function(){
   return $firebaseArray(ref.child('lake'));
 },

//get individual lake activites
 getLake: function(lakeId){
  return $firebaseObject(ref.child('lake').child(lakeId));
}

My app Controller then gets the data through the NoteStore and calling a function "getAllLakes():

app.controller('LakeActivitiesListController', function($scope, NoteStore,              $localStorage) {
$scope.alllakes = NoteStore.getAllLakes();
}

The main problem is that I want to access individual objects from the "lake" table in firebase so that I can be able to store them in localStorage.

This then displays array of objects as below: array of objects hidden

When I just create an object as shown below and save to localStorage, it works fine, as shown in the image below.

var myObj = [
{
  name: 'james',
  title:'doctor'

},
{
  name: 'jojo',
  title:'doctor'

}
];
console.log(myObj);


//
window.localStorage['lakes'] = JSON.stringify(myObj);

$scope.data = JSON.parse(window.localStorage.getItem('lakes'));

stored obj in localStorage

this works fine

I am working on my college project that is overdue. Please help. All I want is to be able to access an array that contains objects from firebase so that I can store them in localStorage. Please.

share|improve this question
    
Can you show what you tried so far? So that we better understand your problem? – kabaehr Apr 14 at 17:48
    
Hi, I tried these options: ` $scope.storeData = function(){ $localStorage.data = $scope.alllakes; } //load data $scope.loadData = function(){ $scope.data = $localStorage.data; } $scope.doRefresh =function() { $scope.loadData(); $scope.$broadcast('scroll.refreshComplete'); }` and also var lakeData = alllakes.getAllLakes(); var data = {}; data = lakeData; for ( var i = 0; i < data.length; i++) { var obj = data[i]; console.log(obj); } – user3225573 Apr 14 at 18:12
    
Can you post the result from the firebase query? – Evans Dianga Apr 14 at 18:47
    
@ Evans Dianga. I have data pulled from firebase fine and I also have ng-repeat in my index.html that does data binding fine and displays all the fields from firebase table on the app. link – user3225573 Apr 14 at 19:07

angular.forEach is the way, here is an example:

angular.forEach(values, function(value, key) {
    console.log(value);
    console.log(key);
});

What your want to do here is to make an angular.forEach on $scope.alllakes and then push the object that you want in the storage

share|improve this answer

The problem is, that you have an object not an array. You can loop through the different members of you object with

for(var propertyName in myObject) {
   // propertyName is what you want
   // you can get the value like this: myObject[propertyName]
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.