TypeError: Cannot read property 'push' of undefined, JavaScript

前端 未结 2 1312
野的像风
野的像风 2021-01-12 10:24

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.



        
2条回答
  •  迷失自我
    2021-01-12 11:16

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

提交回复
热议问题