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

I'm working in this Angular project where user submits a comment form, and the new comment is added to the comments that's already posted. Here is my code.

.controller('productCtrl', function($scope, $http, $routeParams, Page){
$scope.product = {};
$scope.review = {};
$scope.comments = {};

routeparm = $routeParams.param;

$scope.review = function(){
    var review_box = $scope.review_form.review_box;

    $http.post('./comment.php', {
        comment : review_box,
        code: routeparm
    })

    .success(function(data){
            $scope.comments.push(data.comments);
            $scope.review.review_box = '';
    })

    .error(function(data){
        $scope.has_error = true;
        $scope.error_message = data;
    })

};

However when I try to add a comment I get the following error.

TypeError: Cannot read property 'push' of undefined

I've defined an empty $scope.comments = {}; so why am I getting this error? And how can I fix it? Thanks

share|improve this question
3  
you cannot use push for json object. But this will work $scope.comments = [ ]; if you initialize this as array. – ramamoorthy_villi Apr 9 '15 at 4:08
1  
First of all, why push is being using on an object ? – Vigneswaran Marimuthu Apr 9 '15 at 4:09
up vote 10 down vote accepted

The comments you have declared is an Object. Just change your declaration to an array ,

From:

$scope.comments = {};

To:

$scope.comments = [];

EDIT: If you need to Push new Object, You need to make your Comments as Array of Object like this and push the new Object

$scope.comments = { 
    1: {name:'',review:'',comment:'',uptime:'',gravatar:''}
}

 $scope.comments.push({name:'rukshi', review:'test comment',comment:'yet another comment',uptime:'',gravatar:''});
share|improve this answer
    
Still getting the error, the json data looks like this {"comments":{"name":"rukshi","review":"test comment","uptime":1428553143000,"gravatar":"http:\/\/www.gra‌​vatar.com\/avatar\/c‌​a1a69438a32fd2aa6080‌​6b90a960753?s=48"},"‌​error":{"has_error":‌​false}} – rksh Apr 9 '15 at 4:21
    
Yes, there was another problem in the part of the code that haven't posted which sets $scope.comments to undefined, fix that and with your fix everything works fine now. Thanks – rksh Apr 9 '15 at 5:01

You may try this

.controller('productCtrl', function($scope, $http, $routeParams, Page){
  $scope.product = {};
  $scope.review = {};
  $scope.comments = [];

  routeparm = $routeParams.param;

  $scope.review = function(){
  var review_box = $scope.review_form.review_box;

$http.post('./comment.php', {
    comment : review_box,
    code: routeparm
}).success(function(data){
        $scope.comments.push(data.comments);
        $scope.review.review_box = '';
}).error(function(data){
    $scope.has_error = true;
    $scope.error_message = data;
})

};
share|improve this answer

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.