Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS + Mongo DB: ValidationError: User validation failed: username: Path `username` is required

When I am trying to Save/Create a New Document (User) to Mongo DB using Mongoose, I am getting the following Validation Error in spite of providing all the Values with proper DataType:

ValidationError: User validation failed: username: Path username is required.

I am a Beginner at Node JS and Mongo DB. Hence I am not able to understand what is going wrong.

I have also added the following modules:

  • Express
  • Mongo
  • Mongoose
  • Body Parser

Please see below for more details:

Detailed Error:

{
"errors": {
    "username": {
        "message": "Path `username` is required.",
        "name": "ValidatorError",
        "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "username"
        },
        "kind": "required",
        "path": "username",
        "$isValidatorError": true
    }
},
"_message": "User validation failed",
"message": "User validation failed: username: Path `username` is required.",
"name": "ValidationError"
}

User Schema / Model:

var mongoose = require("mongoose");

var userSchema = new mongoose.Schema({
firstName: {
    type: String,
    required: true
},
lastName: {
    type: String,
    required: true
},
username: {
    type: String,
    unique: true,
    required: true
},
password: {
    type: String,
    required: true
}
});

mongoose.model("User", userSchema, "users");

User Controller:

var register = function (req, res) {

console.log("Registering User.");

var firstName = req.body.firstname;
var lastName = req.body.lastname;
var userName = req.body.username;
var password = req.body.password;

User
    .create({
        firstName: firstName,
        lastName: lastName,
        userName: userName,
        password: password
    }, function (err, user) {
        if (err) {
            console.log("Error creating User: ", err);
            res
                .status(400)
                .json(err)
        } else {
            console.log("User Created: ", user);
            res
                .status(201)
                .json(user)
        }
    })
};

I just don't understand, why I am getting this Validation Error. Can anybody please guide me through this. Thanks for the help.

like image 951
Ankit Prajapati Avatar asked Oct 17 '17 17:10

Ankit Prajapati


1 Answers

You made a typo:

User
    .create({
        firstName: firstName,
        lastName: lastName,
        username: userName, // the property is "username" in model
        password: password
    }, function (err, user) {
        if (err) {
            console.log("Error creating User: ", err);
            res
                .status(400)
                .json(err)
        } else {
            console.log("User Created: ", user);
            res
            .status(201)
            .json(user)
        }
    })
};
like image 143
Muhammad Faizan Avatar answered Sep 22 '22 07:09

Muhammad Faizan