最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - TypeError: Cannot read property 'push' of undefined, JavaScript - Stack Overflow

matteradmin15PV0评论

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 = {};
$scopements = {};

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){
            $scopements.push(dataments);
            $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 $scopements = {}; so why am I getting this error? And how can I fix it? Thanks

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 edited Apr 9, 2015 at 4:09 rksh asked Apr 9, 2015 at 4:07 rkshrksh 4,05010 gold badges52 silver badges72 bronze badges 2
  • 3 you cannot use push for json object. But this will work $scope.comments = [ ]; if you initialize this as array. – ramamoorthy_villi Commented Apr 9, 2015 at 4:08
  • 1 First of all, why push is being using on an object ? – Vigneswaran Marimuthu Commented Apr 9, 2015 at 4:09
Add a comment  | 

2 Answers 2

Reset to default 10

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:''});

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;
})

};

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far