0

I am trying to save the attributes of my objects (events) from the backend into new variables for a calendar. Here's my code:

EDIT:

function getEvents(object){

        TimeSlotsModel.all()
            .then(function (result) {
                vm.data = result.data.data;
                 angular.forEach(vm.data, function(value,key) {

        angular.forEach(value, function(value,key) {


            var id = value;
            var name =  value;
            var location = value;
            var allDay = value;
            var startDay = value;
            var endDay = value;
            var startTime = value;
            var endDay = value;
            var user = value;
            console.log(id);
            })
            })
            })
         }

And it has the following output in console:

controllers.js:159 Object {id: "20", descriptives: Object, dates: Object}
controllers.js:159 20
controllers.js:159 Financial Management
controllers.js:159 
controllers.js:159 SR22
controllers.js:159 null
controllers.js:159 2015-12-30T16:00:00.000Z
controllers.js:159 59400
controllers.js:159 66600
controllers.js:159 
controllers.js:159 Object {id: "21", descriptives: Object, dates: Object}
controllers.js:159 21
controllers.js:159 Accounting II
controllers.js:159 
controllers.js:159 SR 2
controllers.js:159 null
controllers.js:159 2015-12-29T16:00:00.000Z
controllers.js:159 2015-12-30T16:00:00.000Z
controllers.js:159 14400

What should I do such that I can save these individual fields into my new variables?

cornstar94
  • 57
  • 2
  • 11
  • TimeSlotsModel.all() is waiting for all the promise(s) to resolve. When you are printing out the variables the promise(s) have not resolved yet. Put your console log code in the TimeSlotsModel.all().then() function – Danny Dec 30 '15 at 15:10
  • Thanks @Dan, I've followed your suggestion and now my variables are no longer null. However, what do you suggest I do to store these params into my new variables? Thanks. – cornstar94 Dec 30 '15 at 15:28
  • Can you show us what the raw JSON looks like? I have an idea of what you're looking for but I don't see where the Object output is coming from. – Marcidius Dec 30 '15 at 15:38
  • Also, what are the keys coming back, are they named accordingly? If so, you could make this a lot easier on yourself which I will demonstrate once I know these answers. – Marcidius Dec 30 '15 at 15:44

1 Answers1

2

If you have respective names as the keys in the JSON:

function getEvents(object){

    TimeSlotsModel.all()
        .then(function (result) {
            vm.data = result.data.data;
        })
 }

You would then be able to get hold of this data (to test this theory) by calling vm.data[0].id, vm.data[0].name, vm.data[0].location to test. If you have these in an ng-repeat:

<div ng-repeat="event in events track by event.id">
  <span>{{event.id}}</span>
  <span>{{event.name}}</span>
  <span>{{event.location}}</span>
  ......
</div>

---- If you don't have appropriate names coming back as the keys in the JSON:

function getEvents(object){
    var singleEvent = {};
    var events = {}
    TimeSlotsModel.all()
      .then(function (result) {
        vm.data = result.data.data;
        angular.forEach(vm.data, function(value,key) {

          angular.forEach(value, function(value,key) {

            singleEvent.id = value;
            singleEvent.name =  value;
            singleEvent.location = value;
            singleEvent.allDay = value;
            singleEvent.startDay = value;
            singleEvent.endDay = value;
            singleEvent.startTime = value;
            singleEvent.endDay = value;
            singleEvent.user = value;
            console.log(singleEvent.id);
         })
         events += singleEvent;
         singleEvent = {};
      })
    })
}
Marcidius
  • 249
  • 1
  • 5