我无法通过邮递员发布 objectID

时间:2021-05-09 15:32:36

标签: node.js mongoose postman

我正在尝试通过 Postman 传递一些数据,但是当我尝试 POST 时,会弹出一个错误。错误如下(当我尝试发布数据时):

Error: Failed to lookup view "error" in views directory "C:\Users\user\Desktop\Blog Project\app\views"
   at Function.render (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\application.js:580:17)
   at ServerResponse.render (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\response.js:1008:7)
   at C:\Users\user\Desktop\Blog Project\app\app.js:50:7
   at Layer.handle_error (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\layer.js:71:5)
   at trim_prefix (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:315:13)
   at C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:284:7
   at Function.process_params (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:335:12)
   at next (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:275:10)
   at Layer.handle_error (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\layer.js:67:12)
   at trim_prefix (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:315:13)
   at C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:284:7
   at Function.process_params (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:335:12)
   at Immediate.next (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:275:10)
   at Immediate.<anonymous> (C:\Users\user\Desktop\Blog Project\app\node_modules\express\lib\router\index.js:635:15)
   at processImmediate (internal/timers.js:463:21)

这是我要传递的数据(我知道导致此问题的问题是作者值,因为我已尝试删除它并传递数据并且它可以正常工作。:

>
{
    "title": "This is new message!",
    "content": "So232ic",
    "published": "true",
    "author": "6097ea05631bde908900b5e2"
}

这是我的控制器以及我想用这个 API 执行的操作:

exports.create_posts = [


  (req, res, next) => {
    const errors = validationResult(req);
    if(!errors.isEmpty()) return res.json(errors.array());

    const {title, content, published} = req.body;
    Posts.create({title, content, published, author: req.user._id}, (err, posts) => {
      if(err) return res.json(err);
     posts.populate('author', (err, populatedPost) => {
       res.json(populatedPost);
    })
  })
}
]

这是我的模型和我拥有的数据库值:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const PostSchema = new Schema({
  title: {type: String, required: true},
  content: {type: String, required: true},
  author: {type: Schema.Types.ObjectId, ref: 'User'},
  published: {type: Boolean, default: true}
}, {timestamps: true})

module.exports = mongoose.model('Post', PostSchema);

这不是数据库连接问题,因为数据库连接正确,日志显示清楚。

1 个答案:

答案 0 :(得分:0)

似乎是渲染名为“error”的视图的问题,似乎无法在

中找到该文件

“C:\Users\user\Desktop\Blog Project\app\views”

如果数据到达您的数据库似乎不是 mongo 错误,而是您的路由有问题,您是否检查了 mongo