这是我的数据库架构的一部分
var commentSchema = new mongoose.Schema({
comment: {
type: String,
required: true
},
commentOwner: {
type: String,
required: true
}
});
这是我的angularjs控制器的一部分
vm.addComment = function () {
var id = $routeParams.id;
var comment = vm.myComment;
console.log(comment);
var postData = {
comment: comment,
commentOwner: own
};
console.log(postData);
postFactory.postComment(id, postData).then(function (response) {
if (response.status === 200) {
$route.reload();
toastr["success"]("Basic Info Saved Successfully");
}
}).catch(function (error) {
// console.log(error);
toastr["error"]("unsuccessful.Please try again!");
});
}
这是我的工厂
function postComment(postId, comment){
return $http.post('http://localhost:8080/api/posts/' + postId + '/comments', comment).then(complete).catch(failed);
}
function complete(response) {
return response;
}
function failed(error) {
console.log(error);
}
我的html部分就是这样
<form name="vm.commentForm" ng-submit="vm.addComment()">
<div class="col-md-9">
<input type="text" class="form-control" placeholder="Add your comment here" required
ng-model="vm.myComment" aria-label="comment" aria-describedby="sizing-addon1">
</div>
<button style="margin:15px;" type="submit" class="btn btn-outline-primary">
<i class="fa fa-send"></i> Send
</button>
</form>
这是我的快速路线控制器
module.exports.addOneComment = function (req, res) {
var id = req.params.postId;
console.log('Add new comment', id);
Post
.findById(id)
.select('comments')
.exec(function (err, doc) {
var response = {
status: 200,
message: doc
};
if (err) {
console.log("Error finding the post");
res.status(500).json({ "message": err });
} else if (!doc) {
console.log("PostId not found in database", id);
res.status(404).json({ "message": "PostId not found in database" });
}
if (doc) {
_addComment(req, res, doc);
} else {
res
.status(200)
.json({ "message": "Comment Added!" });
}
});
}
var _addComment = function (req, res, doc) {
doc.comments.push({
comment: req.body.comment,
commentOwner: req.body.commentOwner
});
doc.save(function (err, postUpdated) {
if (err) {
res
.status(500)
.json(err);
} else {
res
.status(200)
.json(postUpdated.comments[postUpdated.comments.length - 1]);
}
});
}
当我尝试使用postman进行发布时,它工作正常,但是我无法自行发布来自应用程序的数据,并生成错误消息“内部服务器错误”。所以你们能告诉我我的错误是什么吗?非常感谢你!
答案 0 :(得分:0)
在此代码中,我看到的唯一可能性是doc.save
。尝试在console.error(err)
回调中添加doc.save
,以获取更多信息。