0

I am implementing an API for a task management and has two endpoints users and tasks. The schema design for the 'users' looks like:

var userSchema = new mongoose.Schema({
    name: { 
        type:String,
        required: true
    },

    email: {
        type: String,
        required: true,
        unique: true
    },

    pendingTasks: [String],

    dateCreated: { 
        type: Date, 
        default: Date.now }
});

and my POST looks like

router.post('/', function(req, res){
    var userPost = {
        name: req.body.name,
        email: req.body.email
    }
    user.create(userPost, function(err, users){
        if(err) {
            res.status(500).send({
                message:err,
                data: []
            });
        } else {
            res.status(201).send({
                message: 'OK',
                data: users
            });
        }
    });
});

When I try to test my POST on postman with a dummy data by doing

enter image description here

I am getting a "ValidatorError" and it gives me a message that says "User validation failed: email: Path 'email' is required, name: Path: 'name' is required.

enter image description here

This seems weird because I just passed in dummy data 'name' and 'email'.

I am new to creating RESTful API so I might be making a stupid mistake, but I really don't get it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • From the information your provided, I have 2 suggestion: (1) Debug to check what is in "req.body.email". (2) Make sure to install "body-parser" moudle, this article can help you understand what does this module actually do for you: https://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express – yellowB Nov 02 '17 at 05:22
  • Thanks again. I am pretty sure body-parser is installed because I've used it with my previous schema. How do I debug to check what is in the req.body.email in the postman? Is there a good way to do this? – Dawn17 Nov 02 '17 at 05:27

1 Answers1

3

I have also deployed an app in my local PC and debug, if you select raw as your body type, the email property will NOT be exist in the req.body object.

To solve this problem in Postman, you have to select x-www-form-urlencoded and input your data as key-value pairs: enter image description here

yellowB
  • 2,900
  • 1
  • 16
  • 19