2017-08-08 16 views
0

NodeJS、PassportJS、MySQL、Sequalize(MySQLのORM)を使用しています。このコードは私のPassport.JSファイルからのものです。ユーザー名または電子メールが取得された場合にユーザーが自分のWebサイトに登録すると、エラーが返されます。ユーザー名と電子メールの両方がデータベースに見つからない場合は、新しい作成アカウントが作成されます。未処理の拒否TypeError:ヌルのプロパティ 'username'を読み取れません

エラー私は私のウェブサイト上で登録するとき、私は取得しています...

未処理の拒絶はTypeError:ヌルでヌル のプロパティ「ユーザ名」を読み取ることができません。 (/home/ubuntu/workspace/Authentication.1/config/passport/passport.js:50:16) 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/)/proc/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/) (/home/ubuntu/workspace/Authentication.1/node_modules/sequelize/node_modules/bluebird)でを即時に実行します。 /js/release/async.js:17:14)

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

    // GETTING ERROR HERE. If username is already in database 
    if(user.username !== null && user.username == req.body.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.email !== null && user.email == req.body.email) { 
     return done(null, false, console.log("EMAIL TAKEN"),{message : 'That email is already taken'}); 
    } 

    else [code for create new account]... 
+1

'user'はヌルであり、ユーザー名ではありません。 – Li357

答えて

2

あなたの問題は、あなたのif文の最初のチェックからです。ユーザーが見つからない場合は、userがNULLになり、user.username !== nullというステートメントは、取得したエラーメッセージになります。

プロパティではなくオブジェクト全体に対してヌルチェックを実行する必要があります。 if(user!== null && user.username == req.body.username){...}

関連する問題