【问题标题】:error 500 after inserting the data to mongo将数据插入mongo后出现错误500
【发布时间】:2018-12-17 23:30:14
【问题描述】:

我用 mongo 和 express 构建 Vue 应用程序。 将发布请求发送到 mongo 后出现错误。

Uncaught (in promise) Error: Request failed with status code 500
at createError (createError.js?2d83:16)
at settle (settle.js?467f:18)
at XMLHttpRequest.handleLoad (xhr.js?b50d:77)

毕竟我仍然可以发出这个帖子请求,但它不会重定向我。我需要在我的前端应用程序中更改路线,然后我可以看到这个发布的数据。 我认为问题出在我的后期路线设置和猫鼬模型中。

发布路线:

router.route('/').post(function (req, res) {
let post = new Post(req.body);
post.save()
post.comments.push(Comment)
  .then(() => {
    res.status(200).json({});
  })
  .catch(() => {
    res.status(400).send("unable to save to database");
  }) 

})

后模型:

const Post = new Schema({
  title: {
    type: String
  },
  text: {
    type: String
  },
  img: String,
  price: Number,
  postType: String,
  createdAt: { type: Date, default: Date.now },
  comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
},{
    collection: 'posts'
});

评论模型:

const CommentSchema = new Schema({
  text: String,
  rating: Number,
  author: String,

});

对不起我的英语。希望你能理解我

【问题讨论】:

  • 好吧,我是白痴。我删除了这一行 post.cmets.push(Comment) 并且一切正常......我讨厌自己:S

标签: javascript mongodb vue.js


【解决方案1】:

错误 500 表示内部服务器错误。因此,它可能不是来自 Vue,而是来自 express API。你检查过 Express 的日志吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2023-03-16
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    相关资源
    最近更新 更多