AngularJS发布请求未定义

时间:2019-09-05 21:25:48

标签: node.js angularjs mongodb express post

我正在发出发布请求,以将评论插入到我的MongoDB数据库中。尝试进行此插入时出现错误500。我认为这是由于发出请求时未收到该值。发布这些值时哪里出问题了?

app.post('/addComment', (req, res) =>{
  console.log(req.body.description, typeof(req.body.description));
  var com = new Comment({
    description: req.body.description,
    sentiment: ml.classify(req.body.description),
    status: req.body.status,
  });

  com.save(function(err, insComment){
    if(err){
      console.log('Error loading data');
    }
    else{
      res.send(insComment);
    }
  });
});

Controllers.js

exports.CommentController = function($scope, $http){
  $scope.addComment = function(){
    $http({
      method :'POST',
      url: '/addComment',
      headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'},
      data:{
        //username:$scope.username,
        description: $scope.description,
        //sentiment: $scope.sentiment,
        status:$scope.status,
      }
    }).then(function (response){
      $scope.user = response;
      console.log('DESCRIPTION ' + typeof($scope.description))
      //$scope.user = response;
    }, function(err){
      console.log(err);
    })
  }
}

$scope.addComment = function(){
  $http.post('/addComment', {
    description: $scope.review.description,
    status: $scope.review.status,
  },
  {headers: {'Content-Type': 'application/json'}}).
  success(function(data){
    $scope.user = data;
    console.log('DESCRIPTION 2 ' + typeof($scope.description))
    console.log(data);
  }).error(function(err){
    console.log(err);
  });
};

前端

<div class="row p-b-25">
  <div class="col-12 p-b-5">
    <input type="text" 
      ng-model="review.description">

  </div>
  <add-comment></add-comment>
</div>

1 个答案:

答案 0 :(得分:0)

请添加完整的网址,然后尝试。

e:g

 $http({
  method :'POST',
  url: 'http://localhost:3000/addComment',