2017-08-08 51 views
-1

我正在使用NodeJS,PassportJS,MySQL和Sequalize(ORM for MySQL)。此代码来自我的Passport.JS文件。当用户在我的网站上注册并且使用用户名或电子邮件时,我将返回一个错误。如果在数据库中找不到用户名和电子邮件,将创建一个新的创建帐户。如果语句运行但条件不符合

但是,创建新帐户的else语句永远不会运行。当我使用未接收的电子邮件和用户名创建新帐户时,会发生此错误。

未处理的拒绝类型错误:无法读取未定义的属性'用户名' 为空。 (/home/ubuntu/workspace/Authentication.1/config/passport/passport.js:59:21) at tryCatcher(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release /util.js:16:23) at Promise._settlePromiseFromHandler(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:512:31) at Promise。 _settlePromise(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0(/home/ubuntu/workspace/Authentication.1/ node_modules/sequelize/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise .js:693:18) at Async._drainQueu e(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues(/home/ubuntu/workspace/Authentication.1/ node_modules/sequelize/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues [as_onImmediate](/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird /js/release/async.js:17:14) 在processImmediate [按_immediateCallback](timers.js:396:17)

// SELECT * FROM users WHERE username = username || email = ... 
User.findAll({ 
    where: { 
     $or: [{username: username}, {email: req.body.email}] 
    } 
}).then(function(user){ 

// console.log('===================='); 
// console.log(user); 
// console.log(user[0].username); 
// console.log(req.body.username); 
// console.log(user[0].email); 
// console.log(req.body.email); 
// console.log('===================='); 

// If a user is returned from the database run this if statement 
if(user != null) { 
    // GETTING ERROR HERE. If username is already in database return err 
    if(user[0].username == req.body.username) { **//THIS LINE CAUSE ERROR ** 
    console.log(user[0].username); 
    return done(null, false, console.log("USER TAKEN"),{message : 'That username is already taken'}); 
    } 

    // If email is already in database return err. 
    else if(user[0].email == req.body.email) { 
    return done(null, false, console.log("EMAIL TAKEN"),{message : 'That email is already taken'}); 
    } 

} 


else CREATE NEW ACCOUNT... // this never runs for some reason 

> ENTIRE PASSPORT.JS FILE

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


    {   
    usernameField : 'username', 
    passwordField : 'password', 
    passReqToCallback : true // allows us to pass back the entire request to the callback 
    }, 


    function(req, username, password, done){ 
    var generateHash = function(password) { 
     return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null); 
    }; 

    User.findAll({ 
     where: { 
      $or: [{username: username}, {email: req.body.email}] 
     } 
    }).then(function(user){ 

    // console.log('===================='); 
    // console.log(user); 
    // console.log(user[0].username); 
    // console.log(req.body.username); 
    // console.log(user[0].email); 
    // console.log(req.body.email); 
    // console.log('===================='); 


    if(user != null) { 
     if(user[0].username == req.body.username) { 
     console.log(user[0].username); 
     return done(null, false, console.log("USER TAKEN"),{message : 'That username is already taken'}); 
     } 


     else if(user[0].email == req.body.email) { 
     return done(null, false, console.log("EMAIL TAKEN"),{message : 'That email is already taken'}); 
     } 

    } 


    else 
    { 
     var userPassword = generateHash(password); 
     var data = 
     { 
     username: username, 
     password: userPassword, 
     email: req.body.email 
     }; 
+0

在'findAll'中你使用'username',为什么它突然变成'req.body.username',并且你包含了一个bodyparser? – adeneo

+0

你是否100%确定'user'是一个数组并且它至少有一个元素?也许'findAll'返回一个空数组而不是null – litelite

+0

@adeneo因为PassportJS只允许用户名和护照字段。我自己创建一个电子邮件字段,所以我必须使用req.body.username。 是的,我确定它是一个数组。 – Pathway

回答

4

用户是一个数组如果使用用户[0]

的条件是为此:

if(user != null && user.length > 0) { 
0

它看起来像你在任一用户检查[0]的情况下.username或req.body.username当它不存在。如果是这样的话,你可以在你的if语句添加额外的检查,以确保您不会在案件检查用户名在更高层次键不存在,如:

if (user[0] && req.body && (user[0].username == req.body.username)) { 
     // your code here 
    } 

我会检查线路该错误正在引用以确定它是用户还是身体具有未定义的用户名。否则,正如adeneo提到的那样,您可能没有运行body-parser。