The below code is offered on many tutorials (e.g. https://node-postgres.com/#getting-started and https://node-postgres.com/apis/client):
const { Client } = require('pg')
const client = new Client({ user: 'postgres', password: 'postgres', database: 'postgres' })
await client.connect();
// do something
await client.end()
However, it doesn’t work; when I do node mytest.js
it gives:
await client.connect()
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
What I am doing wrong compared with the tutorials?
I tried this:
client.connect();
save(client, '1', '1', '1', '1')
client.end()
function save(client, playerId) {
const res = client.query(`INSERT INTO testtable (player_id)
VALUES ($playerId)`, [playerId])
}
But this gives:
Error: Connection terminated