1

Hi I have got a data in LocalStorage as JSON string:

[
     {"Date":"28/04/2016","Time":"08:00","Title":"Title 1"},
     {"Date":"28/04/2016","Time":"08:30","Title":"Title 2"}
]

And my module.factory looks like:

module.factory('$schedule', function() {
        var schedule = {};

        var result = JSON.parse(localStorage.getItem('myAgenda'));

        schedule.items = [{
                title: result.Title,
                date: result.Date,
                time: result.Time           
        }];
        return schedule;
    });

When I am trying to get data it returns undefined. When I try to get a specific object like:

console.log(result[0].Title);

It works fine and shows only the first element. I guess I missing each definition but don't know how to do it. Please help me to get all results in my schedule.items.

And I am passing the result as items into:

module.controller('ScheduleController', function($scope, $schedule) {    
  $scope.items = $schedule.items; 
});

Many thanks.

6
  • How are you trying to retrieve the data? Commented Mar 15, 2016 at 15:43
  • Is the format correct? [{"Date":"28/04/2016","Time":"08:00","Title":"Title 1"},{"Date":"28/04/2016","Time":"08:30","Title":"Title 2"] or are you missing a }? Commented Mar 15, 2016 at 15:44
  • Hi @LukaJacobowitz I am using the controller: module.controller('ScheduleController', function($scope, $schedule) { $scope.items = $schedule.items; }); Commented Mar 15, 2016 at 15:46
  • You should iterate the array, where now you are not doing so, since you got undefined ! Commented Mar 15, 2016 at 15:46
  • 1
    @qqruza: late to the party! hope you sort it out already from chris answer Commented Mar 16, 2016 at 6:26

3 Answers 3

1

You are trying to access fields in an array without mentioning wich array element you want to access. If you want to enumerate all agenda entries and add them to your array, it should look something like this:

module.factory('$schedule', function () {
    var schedule = [];

    var result = JSON.parse(localStorage.getItem('myAgenda'));

    result.forEach(function (date) {
        schedule.push({
            title: date.Title,
            date: date.Date,
            time: date.Time
        })
    })

    return schedule;
});
Sign up to request clarification or add additional context in comments.

7 Comments

I have got an error: Cannot read property 'push' of undefined
If you look at the line of code "var schedule = {};" i changed it into "var schedule = [];" wich indicates an empty array instead of an empty object. That way you'll be able to use push!
I have done this change but still receive the error TypeError: Cannot read property 'push' of undefined pointing to this line: schedule.items.push({
My appolegies, schedule.push({ it has to be and not schedule.items.push({
Thanks a million @Chris
|
1

You should use .map over array, also add missing } in your last element of array.

var schedule = [];
//assuming result returns an array.
schedule = result.map(function(value){
   return {
        title: value.Title,
        date: value.Date,
        time: value.Time
   };
})

Comments

0

Not familiar with module.factory, but it looks like result is an array of objects and you're accessing it like a single object when creating schedule.items.

You might have to iterate over the array and create an item per object in result.

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.