0

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

    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

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

谢谢!

4

1 回答 1

0

想通了,答案如下:

    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;
于 2018-07-23T16:39:56.853 回答