2

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
                    };
 this.checkedInterviews.push(JSON.stringify(interviewModel));

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

5
  • You've misspelled the Array when you've declared it. You're missing an I in intervIews. Commented Sep 16, 2016 at 10:34
  • because I handwritten that part right now but it's correct in the code Commented Sep 16, 2016 at 10:36
  • could you please give us more code? It seems like your function doesn't have access to this.checkedInterviews. Commented Sep 16, 2016 at 10:37
  • looks like this in this.checkedInterviews.push isn't the same this as the this when you this.checkedInterviews = [] - as your code is extremely sparse, it's hard to say why Commented Sep 16, 2016 at 10:37
  • can you provide more code so we can check is the reference for this.checkedInterviews = [] is correct Commented Sep 16, 2016 at 10:39

4 Answers 4

3

var checkedIntervews = []
var interviewModel = {};
checkedIntervews.push(JSON.stringify(interviewModel));
console.log(checkedIntervews);

(function arrayPush() {
this.checkedIntervews = [];
var interviewModel = {};
this.checkedIntervews.push(JSON.stringify(interviewModel));
console.log(this.checkedIntervews);
})();

You want to try:

var checkedIntervews = []
var interviewModel = {};
checkedInterviews.push(JSON.stringify(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.

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

4 Comments

Wow ok I still get the error, it doesn't let me add an empty json either
Working snippet added
This doesn't reply to the question. You just created a new checkedInterviews (you mispelled: checkedIntervews) object Array and didn't answer about the this object. Why is this.checkedInterviews equal to undefined?
@TheProHands I've added a snippet with all of these things
1

If they are putting in different functions then this in the second function is a different object.

Comments

1

It seems, second function is using different this.

In Angular Module, you can assign this to some variable, then try to access it from second function.

E.g.:

var vm = this;
vm.checkedInterviews = [];

Now in function you should access it using:

vm.checkedInterviews.push();

1 Comment

This way could lead to memory leak. So you'd better handle carefully.
0

Try this Here is working fiddle

  var myApp = angular.module('myApp',[]);

  function MyCtrl($scope) {
  $scope.checkedInterviews = [];
  $scope.pendingInterviews = [{'id':1,'status':'pending','location':'abc','start':'start'}];
  for(i= 0; i < $scope.pendingInterviews.length; i++){
      var interviewModel = {
                      interviewId: $scope.pendingInterviews[i].id,
                      status: $scope.pendingInterviews[i].status,
                      location: $scope.pendingInterviews[i].location,
                      start: $scope.pendingInterviews[i].start,
                      hideCheck: null
      };
      console.log(interviewModel);
      $scope.checkedInterviews.push(JSON.stringify(interviewModel));
    }
  console.log($scope.checkedInterviews);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.