ValidationError: User validation failed at MongooseError.ValidationError
ValidationError: User validation failed at MongooseError.ValidationError
我正在尝试创建一个简单的 node.js 应用程序,使用 mongoDb 进行护照本地身份验证并表示为一个框架,但我遇到了问题
每当我尝试使用注册表将数据提交到数据库中时,单击提交后它会立即出现在节点终端中:
这是我的用户模式的样子:
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);
和我的路由器文件:
// 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.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);
});
}
});
});
}));
我是 node.js 以及 mongoDb 的新手,请帮助我
谢谢
原因: 此错误背后的原因是数据库中存储的类型无效。像 mobile 是数字类型,但是如果你传递的值不能转换为数字,那么它会给出同样的错误。
console.log(新用户);在保存用户并检查您在移动字段中传递的值是否可转换为数字之前,因为它的数据类型在您的模式中是数字。
如果 mobile 为“”或未定义或为空,即不可转换为数字,则它将无法使用。如果它的值不存在,则从对象中删除该键。不要传undefined,null or "" or string(不能转成数字).
我正在尝试创建一个简单的 node.js 应用程序,使用 mongoDb 进行护照本地身份验证并表示为一个框架,但我遇到了问题
每当我尝试使用注册表将数据提交到数据库中时,单击提交后它会立即出现在节点终端中:
这是我的用户模式的样子:
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);
和我的路由器文件:
// 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.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);
});
}
});
});
}));
我是 node.js 以及 mongoDb 的新手,请帮助我
谢谢
原因: 此错误背后的原因是数据库中存储的类型无效。像 mobile 是数字类型,但是如果你传递的值不能转换为数字,那么它会给出同样的错误。
console.log(新用户);在保存用户并检查您在移动字段中传递的值是否可转换为数字之前,因为它的数据类型在您的模式中是数字。
如果 mobile 为“”或未定义或为空,即不可转换为数字,则它将无法使用。如果它的值不存在,则从对象中删除该键。不要传undefined,null or "" or string(不能转成数字).