通过callback或mongoose模型validationPassportJS?

我正在为注册和login用户身份validation设置护照。 现在我看到我可以通过callbackvalidation信息,但是我也可以validation用户模型中的信息。 例如,如果我想使所需的电子邮件字段,那么我可以执行以下操作:

validation与护照

// Create the local sign up with Passport passport.use('local-signup', new LocalStrategy({ usernameField : 'userEmail', // Email Field passwordField : 'userPassword', // Password Field passReqToCallback : true }, function(req, email, password, done) { // Find a user with this email User.findOne({ "local.email": email }, function(err, user) { if (err) throw err; // If a user with this email already exists, continue with failureRedirect if (user) { return done(null); } else { // Otherwise create a new user with the email and generate a hashed password User.create({ local: { email: email } }, function(err, user) { if (err) throw err; user.local.password = user.generateHash(password); // Return the user and finish! :) return done(null, user); }); } }); }; })); 

与用户模型

 var userSchema = new Schema({ local: { email: { type: String, unique: true // Make it unique! Handles entire validation }, password: { type: String }, } }); 

哪些是推荐的,为什么?

为什么不兼得? 如果仅使用第二个,则显示消息将很困难

电子邮件地址已存在

因为您需要捕获duplicate key error index ,然后在注册时显示错误消息。 而不是你可以检查,如果电子邮件地址已经存在,这是更明确的,并在注册时显示相应的错误信息在同一时间使用唯一索引将确保没有重复的电子邮件地址条目。