user137717
user137717

Reputation: 2165

Error posting to Node server with Postman

I am having trouble trying to hit one of my endpoints through postman. I'm new to Postman, so I don't know if my error is from improper Postman usage or my server logic.

Here is the post route and the require of another module I made

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

Here is the post module

var db = require('../db')
var Post = db.model('Post', {
    username: { type: String, required: true},
    body: { type: String, required: true},
    date: { type: Date, required: true,
        default: Date.now}
})

module.exports = Post

the file structure is nodeServer/models with db.js, package.json, node_modules and server.js in nodeServer and post.js in models

my Postman request and the error it returns look like this

Postman Screen Shot

There is more error message beneath what's shown in the picture, but it's all path/morepath/path.js kind of stuff and includes some of my system information.

Please help me solve this validation error!

Upvotes: 0

Views: 2338

Answers (1)

mscdex
mscdex

Reputation: 106698

If you are only using bodyParser.json(), then you need to either also add bodyparser.urlencoded() OR select the raw option in Postman, then select JSON (application/json) in the dropdown next the content type buttons, and then paste valid JSON into the body textarea field.

Upvotes: 2

Related Questions