Node-postgres: PG Pool connect using `await` error : Unexpected identifier

Created on 30 Oct 2017  路  7Comments  路  Source: brianc/node-postgres

According to doc https://node-postgres.com/features/connecting

const Koa = require('koa')
const app = new Koa()

const { Pool } = require('pg')

const pool = new Pool({connectionString:"postgresql://shawn:passwordxxxxxx@localhost:5432/dbname"})

const res = await pool.query('SELECT NOW()')
await pool.end()

await client.end()

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

Error Log

Debugging with inspector protocol because Node.js v8.6.0 was detected.
node --inspect-brk=25673 BE/app.js 
Debugger listening on ws://127.0.0.1:25673/60ee7dd1-9214-474b-b475-5f1e1a660e87
Debugger attached.
/Users/shawn/todayinhistory/BE/app.js:9
const res = await pool.query('SELECT NOW()')
                  ^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3
Waiting for the debugger to disconnect...
SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

Most helpful comment

Hi all, this is still an issue (Outdated docs) - as someone picking up postgress tonight and trying to follow along with the docs, I'm still left scratching my head here.

All 7 comments

node version : v8.6.0
yarn : 1.2.1

package.json

{
  "name": "todayinhostory_node",
  "version": "1.0.0",
  "description": "today in history",
  "main": "app.js",
  "repository": "https://bitbucket.org/iShawnWang2333/todayinhistory",
  "author": "Shawn Wang",
  "license": "MIT",
  "scripts": {
    "dev": "node app.js"
  },
  "dependencies": {
    "koa": "^2.3.0",
    "pg": "^7.3.0"
  }
}

What's wrong with my code or development environment ??
Everything seems OK !!!

You can't use await outside an async function.

So the doc is outdated !

Your issue is related to async/await usage and not to this library, but I admit the docs could be better, especially for users unfamiliar with ES2017 features.

Gotcha, thanks, I will try to learn more about this ~

:D

Hi all, this is still an issue (Outdated docs) - as someone picking up postgress tonight and trying to follow along with the docs, I'm still left scratching my head here.

Was this page helpful?
0 / 5 - 0 ratings