【问题标题】:When I try to post data an error message is generated "Internal Server Error"当我尝试发布数据时,会生成一条错误消息“内部服务器错误”
【发布时间】:2018-11-06 14:05:50
【问题描述】:

这是我的数据库架构的一部分

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>&nbsp;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 发布的数据和浏览器发布的数据有什么区别(使用浏览器开发者工具或 Fiddler 检查请求)?
  • 当我使用邮递员发帖时它可以工作,但是当我使用该应用程序时它就不能工作。我不明白这是什么原因。
  • 可以发一下express routes config的代码吗?
  • router .route('/posts/:postId/comments') .post(commentController.addOneComment);commentController 发布在问题上
  • 距离有多远?是否在后端记录“添加新评论”?

标签: angularjs express


【解决方案1】:

在这段代码中,我看到的唯一可能性是doc.save。尝试在您的 doc.save 回调中添加 console.error(err) 以了解更多信息。

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 2022-11-27
    • 2022-08-18
    • 2018-05-20
    相关资源
    最近更新 更多