Mocktheduck Mocktheduck - 1 month ago 8x
AngularJS Question

Cannot push JSON object into Array

I know it seems like a duplicate but it isn't. Nothing worked, whatever I tried.

I have a list in my angular module:

this.checkedInterviews = []

and then a function that does:

var interviewModel = {
interviewId: self.pendingInterviews[i].id,
status: self.pendingInterviews[i].status,
location: self.pendingInterviews[i].location,
start: self.pendingInterviews[i].start,
hideCheck: null

I get
Cannot read property 'push' of undefined
ANy idea what the problem is?


var checkedIntervews = []
var interviewModel = {};

(function arrayPush() {
this.checkedIntervews = [];
var interviewModel = {};

You want to try:

var checkedIntervews = []
var interviewModel = {};
$scope.checkedInterviews = checkedInterviews; //If using AngularJS because Angular is tagged in the question

NOTE: You should be able to use this if all of this is in the same function. This should work in the global scope as well. The only reason why I've used a IIFE is to separate out the scopes

Snippet added above with both cases.

If is not clear what this is in your question btw.