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

I'm "pushing" a new event to the vm.events object and I keep getting the TypeError: Cannot read property 'push' of undefined error.

I spent hours on this and I don't see what is undefined exactly, every element seems to have a value.

This is what I see in my console when I log eventdata:

Object
    actions:    Array[2]
    color:      Object
    draggable:  true
    endsAt:     Fri Dec 16 2016 08:00:00 GMT-0500 (Eastern Standard Time)
    eventid:    457
    resizable:  true
    startsAt:   Fri Dec 16 2016 07:00:00 GMT-0500 (Eastern Standard Time)
    title:      "Asa Kris - Chris Brown "
    __proto__:  Object

js (see vm.eventSaved function)

var actions = [{
  label: '<i class=\'glyphicon glyphicon-pencil\'></i>',
  onClick: function(args) {
    showEventModal('Edited', args.calendarEvent);
    //alert.show('Edited', args.calendarEvent);
  }
}, {
  label: '<i class=\'glyphicon glyphicon-remove\'></i>',
  onClick: function(args) {
    vm.eventDeleted (args.calendarEvent) 
    //alert.show('Deleted', args.calendarEvent);
  }
}

vm.events = [
  {
    title: 'An event',
    color: calendarConfig.colorTypes.warning,
    startsAt: moment().startOf('week').subtract(2, 'days').add(8, 'hours').toDate(),
    endsAt: moment().startOf('week').add(1, 'week').add(9, 'hours').toDate(),
    draggable: true,
    resizable: true,
    actions: actions
  }, {
    title: 'This is a really long event title that occurs on every year',
    color: calendarConfig.colorTypes.important,
    startsAt: moment().startOf('day').add(7, 'hours').toDate(),
    endsAt: moment().startOf('day').add(19, 'hours').toDate(),
    recursOn: 'year',
    draggable: true,
    resizable: true,
    actions: actions
  }
];




   vm.eventSaved = function() { 

       var eventdata = {eventid       : null,
                title         : newAppointmentCustomerId,
                color         : calendarConfig.colorTypes.info,
                startsAt      : new Date(vm.dtStart),
                endsAt        : new Date(vm.dtEnd),
                draggable     : true,
                resizable     : true,
                actions       : actions
                };

       var urlapieventspost = $location.protocol() + "://" + $location.host() + "/api/events";

       $http.post(urlapieventspost, indata).success(function(data, status){

          eventdata.eventid = data.id;          
          eventdata.title   = data.eventtitle;                          

          console.log(eventdata);

          vm.events.push(eventdata);        

          }).error(function(err){
              /* do something with errors */
          });
   };
share|improve this question
    
You should log vm.events, not eventdata. You'll likely find it is vm.events is undefined, because you're not in a different scope. – Mike McCaughan yesterday
    
Ohh man, that makes a lot of sense. Let me check this out. Thanks – user3489502 yesterday
    
It works now, events wasn't passed to eventSaved function, thanks! – user3489502 3 hours ago
up vote 0 down vote accepted

vm.events is undefined. Since it's the only object, for which push is called. Try to debug and log vm.events. Also it would be helpful, if you show, where vm is declared.

share|improve this answer
    
You're right! events wasn't passed to eventSaved function, thanks! – user3489502 3 hours ago

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.