服务器脚本代码无法运行以变量为参数的查询

时间:2019-07-10 16:50:15

标签: node.js database psql

我建立了服务器和数据库。我的问题是有关在数据库上运行的查询的问题:

app.get('/api/plan/download/:id', async (req, res) => {
    try {
        const {rows} = await client.query('select * from nova_schema.files where user_id = ' + req.id + ';');
        res.send(rows);
    } catch (ex) {
        console.log('fail' + ex);
        res.send('fail' + ex);
    }
});

抛出错误“ failerror:“未定义”列不存在”

但这可行:

const {rows} = await client.query('select * from nova_schema.files where user_id = 1;');

我是新手,正在尝试学习。可能是什么问题?

非常感谢!

1 个答案:

答案 0 :(得分:0)

您缺少req.params.id

只需将行更改为

const {rows} = await client.query('select * from nova_schema.files where user_id = ' + req.params.id + ';');