James
James

Reputation: 11

$scope push is not a function

I have some code like:

$scope.offer = [];
function offer (Details) {
  angular.forEach(Details, function (product) {
    $scope.offer = SomeAPI.ById.query({ product: id }, function (response) {
      $scope.offer.push(response);
    });
  });
  console.log($scope.offer);
}

Error in console:$scope.offer.push is not a function.

Upvotes: 1

Views: 1347

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222592

Initialize the array before you push the value,

 $scope.offer = [];
angular.forEach(Details, function (product) {     
    $scope.offer = SomeAPI.ById.query({ product: id }, function (response) {
      $scope.offer.push(response);
    });
  });

Upvotes: 2

Savaratkar
Savaratkar

Reputation: 2084

Why are you assigning '$scope.offer' to return type of API call.

 $scope.offer = SomeAPI.ById.query({ product: id }, function (response) {

This changes the type of '$scope.offer' from '[]' array type to probably a promise object which that api is returning. Thats why push method is not working for you.

Correct code should be:

$scope.offer = [];
function offer (Details) {
  angular.forEach(Details, function (product) {
   SomeAPI.ById.query({ product: id }, function (response) {
      $scope.offer.push(response);
    });
  });
  console.log($scope.offer);
}

Upvotes: 1

Related Questions