简体   繁体   中英

Node/Mongoose Error: ValidationError

This is my server.js. When I run node server.js then use PostMan to post json, it gives me the following error.

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)
  })
})

The error:

(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)

I'm trying to learn the MEAN stack through "Write Modern Web Apps with the MEAN Stack" but I'm running into issues all the time, even when I follow the code and instructions exactly. Can anyone help understand this error, and possibly recommend some good resources for learning the mean stack?

This error is triggered because you have provided a mongoose validation in your schema (in /models/post ) and that validation is failing.

For instance, if you provided your model like this :

var postSchema = new Schema({

    "username": String,
    "body": String,
    "email": {
        type: String,
        required: true
    }

});
var Post = mongoose.model('Post', postSchema);

This would fail because email required validator is not respected. Find a full list of validators here .

Side note : res.sendStatus(201).json(post) will set the json body and content-type header after sending a response with 201 status. To send both use :

res.status(201).json(post)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM