Angular - 对数据库发表评论

时间:2016-12-28 21:19:26

标签: angularjs forms

我正在尝试将输入表单中的注释发布到我的数据库中。我传入的注释当前记录为未定义。该服务没有记录,我得到500服务器错误:user_id违反非空约束。我做错了什么?

HTML(更新)

<div ng-controller="CommentsCtrl as commentsCtrl">
 <div ng-show="makeComment" class="collapsible-header">
     <input ng-model="comment" type="text" name="comment" id="comment_content" class="col s11 m11 l11" placeholder="Make a comment.">
         <a href="" type="submit" class="col s1 m1 l1 black-text comment" ng-click="commentsCtrl.createComment(comment, userId, postId)"><i class="material-icons">check_circle</i></a>
  </div>

控制器 - CommentsCtrl(已更新)

this.comment = '';

createComment(comment, userId, postId) {
 return this.commentsService.createComment(comment, userId, postId);
 }
}

服务 - 评论服务(已更新)

this.createcomment = $http;
this.commentContent = '';
this.postId;
this.userId;

createComment(comment, userId, postId) {

this.createcomment.post('/comments', { comment: this.commentContent, userId: this.userId, postId: this.postId })
.then((res) => {
  this.comment = res.data;
})
.catch((err) => {
  return err;
});
}

1 个答案:

答案 0 :(得分:0)

您没有传递userId或postId的任何值:

this.commentsService.createComment(comment);

它们是空的。