Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValidationError: User validation failed at MongooseError.ValidationError

I am trying to create a simple node.js app with passport local authentication using mongoDb and express as a framework,but I'm having a problem

Whenever I try to submit the data into database using registration form,after clicking submit it appears in node terminal immediately : node terminal

here is how my user schema looks like:

    var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our user model
var userSchema = mongoose.Schema({

    local            : {
        name    : String,
        username     : String,
        mobile       : Number,
        email        : String,
        gender       : String,
        password     : String

    }
});

// methods ======================
// generating a hash
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
};

// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);

and my router file :

 // process the signup form
    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

passport configuration for sign up logic :

    passport.use('local-signup', new LocalStrategy({

    nameField     : 'name',    
    usernameField : 'username',
    mobileField   : 'mobile',
    emailField    : 'email',
    genderField   : 'gender',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, name, username, mobile, email, gender, password, done) {

    // asynchronous
    // User.findOne wont fire unless data is sent back
    process.nextTick(function() {

    // find a user whose email is the same as the forms email
    // we are checking to see if the user trying to login already exists
    User.findOne({ 'local.email' :  email }, function(err, user) {
        // if there are any errors, return the error
        if (err)
            return done(err);


        // check to see if theres already a user with that email
        if (user) {
            return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
        } else {

            // if there is no user with that email
            // create the user
            var newUser            = new User();

            // set the user's local credentials
            newUser.local.name     = name;
            newUser.local.username = username;
            newUser.local.mobile   = mobile;
            newUser.local.email    = email;
            newUser.local.gender   = gender;
            newUser.local.password = newUser.generateHash(password);

            // save the user
            newUser.save(function(err) {
                if (err)
                    throw err;

                return done(null, newUser);
            });
        }

    });

    });

}));

I am new to node.js as well as in mongoDb please help me out
Thanks

like image 373
Zub Avatar asked Oct 07 '16 01:10

Zub


1 Answers

Reason: reason behind this error is invalid type to store in db. Like mobile is number type but if you are passing a value which can not be convert to number then it will give the same error.

console.log(newUser); before saving the user and check the value you are passing in mobile field is convertible to Number as its data type is number in your schema.

If mobile is "" or undefined or null i.e. not convertible to number then it will not work. Remove this key from object if it's value doesn't exists. Do not pass the undefined,null or "" or string(which can not be convert to number).

like image 124
Sachin Avatar answered Nov 08 '22 16:11

Sachin