服务器脚本代码无法 运行 使用变量作为参数进行查询
Server script code can not run query with a variable as parameter
我建了一个服务器和一个数据库。我的问题是关于数据库上查询 i 运行 的问题:
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: column "undefined" 不存在"
但这行得通:
const {rows} = await client.query('select * from nova_schema.files where user_id = 1;');
我是新手,正在努力学习。可能是什么问题?
非常感谢!
你不见了req.params.id
只需将行更改为
const {rows} = await client.query('select * from nova_schema.files where user_id = ' + req.params.id + ';');
我建了一个服务器和一个数据库。我的问题是关于数据库上查询 i 运行 的问题:
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: column "undefined" 不存在"
但这行得通:
const {rows} = await client.query('select * from nova_schema.files where user_id = 1;');
我是新手,正在努力学习。可能是什么问题?
非常感谢!
你不见了req.params.id
只需将行更改为
const {rows} = await client.query('select * from nova_schema.files where user_id = ' + req.params.id + ';');