Node-postgres: rows returns empty array while executing insert query

Created on 8 May 2018  Â·  1Comment  Â·  Source: brianc/node-postgres

I want to get the inserted ID from INSERT query. But response always empty array. How to get the inserted ID.

const client= new Client({
                host: "host",
                user: "me",
                password: `myPassword`,
                database: "test",
                port: 5432
            });
            client.connect();
            client.query(query, data, (err, res) => {
                client.end();
                if (err) return reject(err);
                return resolve(res);
            });

Insert query returns following response

{
    "command": "INSERT",
    "rowCount": 1,
    "oid": 0,
    "rows": [],
    "fields": [],
    "_parsers": [],
    "RowCtor": null,
    "rowAsArray": false
}
question

Most helpful comment

You’ll need to use INSERT’s RETURNING clause. The documentation is at https://www.postgresql.org/docs/current/static/sql-insert.html and it looks like this:

INSERT INTO a_table (column1, …) VALUES (value1, …) RETURNING id_column

>All comments

You’ll need to use INSERT’s RETURNING clause. The documentation is at https://www.postgresql.org/docs/current/static/sql-insert.html and it looks like this:

INSERT INTO a_table (column1, …) VALUES (value1, …) RETURNING id_column
Was this page helpful?
0 / 5 - 0 ratings

Related issues

frmoded picture frmoded  Â·  3Comments

dipakdas99 picture dipakdas99  Â·  3Comments

gpanainte picture gpanainte  Â·  3Comments

joaquimknox picture joaquimknox  Â·  3Comments

AhmedBHameed picture AhmedBHameed  Â·  3Comments