node-postgres
node-postgres copied to clipboard
Error: Stream unexpectedly ended during query execution
Hi.
When I connect to PgSQL database as in doc:
conString = "postgres://USER:PASS@HOST/DB"
client = new pg.Client(conString)
client.connect (err) ->
if err
return console.error 'could not connect to postgres', err
client.query('SELECT 1', (err, result) ->
if err
return console.error('error running query', err)
console.log(result)
)
client.end()
it already connects to database, but it throws error on query execution:
error running query [Error: Stream unexpectedly ended during query execution]
Credentials is right, in PHP it works well and if they would have been wrong, it would throw me connection error.
I tryed googling but it appears that I am the first one, which is searching for this error.
(node-postgres 2.11)
Any ideas? Thx.
You are ending the client before the query returns. The call to client.query is non-blocking, so it will not necessarily finish before the next line is executed. Try moving the call to client.end() inside the client.query callback function.
Thank you. It helped :smile: