首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点/Mongoose错误: ValidationError

节点/Mongoose错误: ValidationError
EN

Stack Overflow用户
提问于 2016-12-25 21:19:49
回答 0查看 1.8K关注 0票数 0

这是我的server.js。当我运行node server.js然后使用PostMan来post json时,它给出了以下错误。

代码语言:javascript
复制
var express = require('express')
var bodyParser = require('body-parser')
var app = express()

app.use(bodyParser.json())

app.get('/api/posts', function(req, res) {
  res.json([
    {
      username: '@rodandrew95',
      body: 'node rocks!'
    }
  ])
})

app.listen(3000, function() {
  console.log('Server listening on', 3000)
})


var Post = require('./models/post')
app.post('/api/posts', function(req, res, next) {
  // console.log('post received')
  // console.log(req.body.username)
  // console.log(req.body.body)
  // res.sendStatus(201)
  var post = new Post({
    username: req.body.username,
    body: req.body.body
  });
  post.save(function (err, post) {
    if (err) { return next(err) }
    res.sendStatus(201).json(post)
  })
})

错误:

代码语言:javascript
复制
(node:6863) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
ValidationError: Post validation failed
    at MongooseError.ValidationError (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/error/validation.js:23:11)
    at model.Document.invalidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1486:32)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1362:17
    at validate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:705:7)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:742:9
    at Array.forEach (native)
    at SchemaString.SchemaType.doValidate (/Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/schematype.js:710:19)
    at /Users/andrewrodrigues/Desktop/write_modern/ch_1/node_modules/mongoose/lib/document.js:1360:9
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

我试图通过“用MEAN stack编写现代Web应用程序”来学习MEAN堆栈,但我总是遇到问题,即使我完全遵循代码和说明。有没有人可以帮助理解这个错误,并可能推荐一些好的资源来学习mean堆栈?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41321450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档