PostgreSQL错误:where子句中不存在列“”

时间:2019-12-18 11:12:46

标签: javascript sql node.js postgresql koa

我正在尝试检查电子邮件是否已经存在,并且正在通过电子邮件参数发送 daniyal@gmail.com ,但是获取错误列“ daniyal”不存在。

ctx.app.pool.query("SELECT * FROM users WHERE email = " + ctx.request.body.email, (err, result) => {
            if (err) {
                ctx.body = {
                    status: 500,
                    message: 'Something went wrong'
                };
                console.log('Query Error: ', err);
                throw err
            } else {
                ctx.body = {
                    exceptions: "",
                    status: 200,
                    error: false,
                    message: "user already exists with this email",
                };
            }
        });

1 个答案:

答案 0 :(得分:2)

您的直接问题是,您在传递的值周围缺少单引号(因此,Postgres认为它是列名)。

但是,为此,您要做要使用参数化查询,以提高安全性和效率

ctx.app.pool.query(
    "SELECT * FROM users WHERE email = ?", 
    [ctx.request.body.email], 
    function(err, result) => {
        if (err) {
            ctx.body = {
                status: 500,
                message: 'Something went wrong'
            };
            console.log('Query Error: ', err);
            throw err
        } else {
            ctx.body = {
                exceptions: "",
                status: 200,
                error: false,
                message: "user already exists with this email",
            };
        }
    }
);