javascript - 未处理的拒绝 TypeError : Cannot read property 'username' of null

标签 javascript mysql node.js sequelize.js

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

我在网站上注册时遇到的错误是...

Unhandled rejection TypeError: Cannot read property 'username' of null
    at null.<anonymous> (/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/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._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)
    at Immediate.Async.drainQueues [as _onImmediate] (/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]...

最佳答案

您的问题来自您的 if 中的第一次检查陈述。如果找不到用户,user将为 null 并且语句 user.username !== null将导致您收到错误消息。

您应该对整个对象而不是属性执行空检查。 if(user!== null && user.username == req.body.username){...}

关于javascript - 未处理的拒绝 TypeError : Cannot read property 'username' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559323/

相关文章:

mysql - 获取连接 MySQL 中其他表的两个不同列的平均值

javascript - MongoDB(可能的 _id 数量)

javascript - 单击后移除 Bootstrap 5 切换按钮上的焦点

javascript - 未捕获的类型错误 : undefined is not a function on line var new_arr = filtered. 分割 (',' );

mysql - 为什么选择二进制 (16) PK 而不是复合 key ?

node.js - 莫吉托安装错误

javascript - JSON.stringify 自定义格式

javascript - 从嵌套的对象数组中提取对象

javascript - Select2 jquery 插件显示结果中选定项目的数量而不是标签

mysql - 如何在不登录数据库的情况下从 MySQL 迁移数据?