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

3

7

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.

Disentwine answered 17/10, 2017 at 17:14 Comment(2)
Property names are case sensitive. You're passing in "userName" instead of "username."Vivisect
Ohhhhh God...... That was such a Silly Mistake. Thank you so Much.Disentwine
N
9

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)
        }
    })
};
Nephritic answered 12/3, 2018 at 13:1 Comment(0)
A
0

I did have this problem and the way to solve it just to add these lines in your server.js:

app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());

and make sure to install Cors and Express.

My problem was that I didn't add just this line:

app.use(express.json());

After adding it, data was saved perfectly.

Arleanarlee answered 29/5, 2022 at 13:5 Comment(0)
A
0

In schema you declared username like this

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

while in controller you declared username like this

var userName = req.body.username;

solution:

var username = req.body.username;
Aerugo answered 3/4 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.