Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  125] [ 5]  / answers: 1 / hits: 22344  / 8 Years ago, fri, october 7, 2016, 12:00:00

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



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


More From » node.js

 Answers
7

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).


[#60474] Wednesday, October 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
loric

Total Points: 110
Total Questions: 96
Total Answers: 91

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;