Search code examples
node.jspostgresqlpg-promisenode-postgres

Parsing PostgreSQL response from any() into JS object


When I call:

db.any('SELECT (col1, col2, col3) FROM myTable....[other conditions]')

where db is an instance of pg-promise connection, as a promise result for

.then(function(data)) {

I get an array with objects like { row: '(ans1,ans2,ans3)' }

It is also written in pg-promise documentation

Is there any convenient way (i.e. without string triming and coma splitting) to parse it to JS object? I would like to use it in views (.pug files), for example element.col1 which will print ans1.


Solution

  • By wrapping column names into (), you are specifically requesting exactly what you are getting back.

    Without () you will get an array of JSON objects.