4

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.

4
  • Can you show what you tried so far? So that we better understand your problem? Commented Apr 14, 2016 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); } Commented Apr 14, 2016 at 18:12
  • Can you post the result from the firebase query? Commented Apr 14, 2016 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 Commented Apr 14, 2016 at 19:07

2 Answers 2

6

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

Example with array of objects iteration:

var array_of_objects = [{name: 'Jimi', gender: 'male'},{name: 'Peter', gender: 'male'},{name: 'Bob', gender: 'male'}];

angular.forEach(array_of_objects, function(item, index) {
  console.log(item, index);
});

Example with object iteration:

var my_object = {name: 'Jimi', gender: 'male', age: '25'};

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

In your case you want to make an angular.forEach on $scope.alllakes and then push the object that you want in the storage

Sign up to request clarification or add additional context in comments.

Comments

-1

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]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.