【问题标题】:Postgres results.rows[0] are undefinedPostgres results.rows[0] 未定义
【发布时间】:2019-07-16 03:31:06
【问题描述】:

不知道我做错了什么,除了为我正在学习 Nodejs 的项目编写非常混乱的代码。

这曾经是一个异步函数/对象,但决定摆脱 try catch,因为我的代码出于某种我无法弄清楚的原因运行了两次。

我认为消除 try catch 并没有真正阻止它仍然运行两次。

所以问题是:为什么我的 results.rows[0].email 返回未定义?

有时有效,有时无效。我不知道为什么。任何帮助都会动摇。

   router.post('/', (req, res, next) => {
    const {password, email} = req.body
            //var LoginPwd = await bcrypt.hash(password, 5);
    const loginPlainPwd = password;

    pool.query("SELECT password, id, email FROM companies_admins WHERE email=$1", [email], (err, results) => {
        if (err)
        {
            throw err;
        }

        const dbemail = results.rows[0].email
        const dbPwd = results.rows[0].password 
        const dbid = JSON.stringify(results.rows[0].id)
        console.log('results.rows[0] = ' + results.rows[0])
        console.log('loginPlainPwd = ' + loginPlainPwd)
        console.log('dbPwd = ' + dbPwd)
        //console.log(JSON.stringify(results.rows[0]))
        //res.cookie('userId', id)
        //res.sendFile(path.join(__dirname, './views/account.html'));
        //bcrypt.compare(loginPlainPwd, dbPwd, (err, res) => {
        if (loginPlainPwd != dbPwd) 
        {
            console.log("loginPlainPwd != dbPwd")
            /////////////////////////////////////////////?SHOULD THIS BE OUTSIE POOL.QUERY??????
            console.log('err')
            return res.status(401).json({
                message: 'Auth failed'
            });
        }
        else if (loginPlainPwd == dbPwd) 
        {
            //token variable signage/creation with user data and expiration (i also included .env)
            const token = jwt.sign(
                {
                    email: dbemail,
                    userId: dbid,
                }, 
                process.env.JWT_KEY, 
                {
                    expiresIn: "1h"
                },
            );

            console.log("passwords match: token created:" + token)
            res.cookie('userId', token,)

            console.log('cookie should be sent')
            databaseJWTin(err, token, dbemail); // database function to store jwttoken from below to store jwt in database
            console.log('databaseJWT function should have fired')
            //had to use ../ below because path was going into routes directory for some reason
            res.sendFile(path.join(__dirname, '../views/account.html'))
            //return res.status(200).json({
            //  message: "Auth successful",
            //  token: token
            //});
        }
        //res.sendFile(path.join(__dirname, './views/account.html'))
    });
    //res.sendFile(path.join(__dirname, './views/account.html'));
})

【问题讨论】:

  • 如果查询没有返回结果,似乎显而易见的答案是?
  • 我意识到我的问题是我的:'const {password, email} = req.body' 只是从'req.body' 创建密码。当我更改为单个 const/var 即 'const password = req.body.password const email = req.body.email' 时,它才正确访问正文中的值。
  • 如果您使用的是最新的节点版本,您的第一个语法应该可以工作。你确定没有其他改变吗?

标签: node.js database postgresql express


【解决方案1】:

请检查结果中是否包含数据。

router.post('/', (req, res, next) => {
    const { password, email } = req.body
    //var LoginPwd = await bcrypt.hash(password, 5);
    const loginPlainPwd = password;

    pool.query("SELECT password, id, email FROM companies_admins WHERE email=$1", [email], (err, results) => {
        if (err) {
            throw err;
        }
        if (results && results.length>0) {
            const dbemail = results.rows[0].email
            const dbPwd = results.rows[0].password
            const dbid = JSON.stringify(results.rows[0].id)
            console.log('results.rows[0] = ' + results.rows[0])
            console.log('loginPlainPwd = ' + loginPlainPwd)
            console.log('dbPwd = ' + dbPwd)
            //console.log(JSON.stringify(results.rows[0]))
            //res.cookie('userId', id)
            //res.sendFile(path.join(__dirname, './views/account.html'));
            //bcrypt.compare(loginPlainPwd, dbPwd, (err, res) => {
            if (loginPlainPwd != dbPwd) {
                console.log("loginPlainPwd != dbPwd")
                /////////////////////////////////////////////?SHOULD THIS BE OUTSIE POOL.QUERY??????
                console.log('err')
                return res.status(401).json({
                    message: 'Auth failed'
                });
            }
            else if (loginPlainPwd == dbPwd) {
                //token variable signage/creation with user data and expiration (i also included .env)
                const token = jwt.sign(
                    {
                        email: dbemail,
                        userId: dbid,
                    },
                    process.env.JWT_KEY,
                    {
                        expiresIn: "1h"
                    },
                );

                console.log("passwords match: token created:" + token)
                res.cookie('userId', token)

                console.log('cookie should be sent')
                databaseJWTin(err, token, dbemail); // database function to store jwttoken from below to store jwt in database
                console.log('databaseJWT function should have fired')
                //had to use ../ below because path was going into routes directory for some reason
                res.sendFile(path.join(__dirname, '../views/account.html'))
                //return res.status(200).json({
                //  message: "Auth successful",
                //  token: token
                //});
            }
            //res.sendFile(path.join(__dirname, './views/account.html'))
        }

    });
    //res.sendFile(path.join(__dirname, './views/account.html'));
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2018-11-04
    相关资源
    最近更新 更多