Postgres results.rows [0]未定义

时间:2019-02-22 04:54:28

标签: node.js database postgresql express

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

这曾经是一个异步函数/对象,但由于我的代码由于某种原因而无法运行,因此决定放弃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'));
})

1 个答案:

答案 0 :(得分:0)

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

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