TypeError:无法设置未定义的属性“电子邮件”

时间:2018-11-14 06:17:55

标签: javascript node.js

我正在使用Node.js开发一个项目,并尝试应用密码加密,但仍然收到此错误“ TypeError:无法设置未定义的属性'email'”。 请问有人可以帮我吗?谢谢

var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
 passport.serializeUser(function(user, done){
     done(null, user.id);
 });

 passport.deserializeUser(function(id, done){
     User.findById(id, function(err,user){
         done(err, user);
     });
 });

 passport.use('local.signup', new LocalStrategy({
     usernameField: 'email',
     passwordField: 'password',
     passReqToCallback: true
 }, function(req, email, password, done){
     User.findOne({'email': email}, function(err, user){
         if(err){
             return done(err);
         }
         if(user){
             return done(null, false, {message: 'Email is already in use'});
         }
         var newUser = new User();
         newUser.local.email = email;
         newUser.local.password = newUser.encryptPassword(password);
         newUser.save(function(err, result){
             if(err){
                 return done(err);
             }
             return done(null, newUser);
         });
     });
 }));

2 个答案:

答案 0 :(得分:0)

首先,定义newUser.local,然后定义newUser.local.emailpassword

 var newUser = new User();
         newUser.local = {email:"", password:""};
         newUser.local.email = email;
         newUser.local.password = password;
         newUser.save(function(err, result){
             if(err){
                 return done(err);
             }

答案 1 :(得分:0)

local未定义。试试这个:

var newUser = new User();
newUser.local = {
    email : email,
    password : newUser.encryptPassword(password)
}