Node.js pg客户端查询对第一个查询有效,但对第二个查询失败,尽管有数据



在下面的代码中,第一个查询按预期工作,结果很好。但是,当第二个查询运行时,结果是未定义的,并且在尝试访问行值时抛出错误。我已经在数据库中手动运行了实际的查询,它会返回一个值,这样就有了数据。

var cli = new pg.Client(conn);
await cli.connect();
//get last max log id
const {rows} = await cli.query("SELECT value as id FROM public.mytable  where name = 'max_log_id'");
lastMaxId = rows[0].id; //no error here
console.log(lastMaxId);
//get max log id
const {res} = await cli.query('SELECT max(id) as id FROM public.myothertable');
console.log('RES: ' + JSON.stringify(res)); //res = undefined
maxId = res[0].id; //throws error here

我可以不对同一个客户端运行两个查询吗?还是我需要以某种方式重置客户端?

谢谢!

想清楚了,答案是:

var cli = new pg.Client(conn);
await cli.connect();
//get last max log id
var {rows} = await cli.query("SELECT value as id FROM public.my table  where name = 'max_log_id'");
lastMaxId = rows[0].id;
console.log(lastMaxId);
//get max log id
rows = await cli.query('SELECT max(id) as id FROM public.myothertable');
console.log('RES: ' + JSON.stringify(rows.rows[0].id));
maxId = rows.rows[0].id;

最新更新