如果语句运行但不满足条件

时间:2017-08-08 16:53:12

标签: javascript mysql node.js if-statement passport.js

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

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

未处理拒绝TypeError:无法读取未定义的属性“username”     在null。 (/home/ubuntu/workspace/Authentication.1/config/passport/passport.js:59:21)     在tryCatcher(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)     在Promise._settlePromiseFromHandler(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:512:31)     在Promise._settlePromise(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:569:18)     在Promise._settlePromise0(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:614:10)     在Promise._settlePromises(/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:693:18)     at Async._drainQueue(/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)     在Immediate.Async.drainQueues [as _onImmediate](/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)     at processImmediate [as _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
  

>   整个PASSPORT.JS文件

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
      };

2 个答案:

答案 0 :(得分:4)

如果您使用用户[0]

user是一个数组

条件是:

Team_DELETE

答案 1 :(得分:0)

在它不存在的情况下,您似乎正在检查用户[0] .username或req.body.username。如果是这种情况,您可以在if语句中添加一个额外的检查,以确保在更高层密钥不存在的情况下不检查用户名,如:

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

我会检查错误引用的行,以确定它是用户还是具有未定义用户名的正文。否则,正如adeneo所提到的,你可能没有运行body-parser。