2

As the title says, I'm running into a syntax error using node-postgres. Here's what the code looks like

const {Pool, Client} = require('pg')
const pool = new Pool({
  user: '<user>',
  host: '<host>',
  database: '<database>',
  password: '<pw>',
  port: <port>
})

let query = `SELECT * FROM user JOIN notifications ON user.user_id = notifications.user_id WHERE user_id=$1`

let values = ["123"]

pool.query(query, values)
  .then(() => { /* do something */} )
  .catch((err) => { console.log(err)} )

Based on this query, I get a syntax error with the message

syntax error at or near "."

Since the same query runs fine in pgAdmin, my guess is that it's module specific, but I haven't figured out what the problem is.

Any help much appreciated!

Edit: added missing bracket, thanks to Sreeragh A R

3
  • For me it is working.. Commented Aug 21, 2018 at 15:08
  • 1
    A bracket is missing in last line. Change it to .catch((err) => { console.log(err)} ) Commented Aug 21, 2018 at 15:10
  • 1
    @SreeraghAR thanks, fixed it. However, that was a typo from making it a minimal example. Brackets are correct in original code. Commented Aug 21, 2018 at 15:15

1 Answer 1

5

user is reserved word in postgresql you have to escape user using double quotes

let query = `SELECT * FROM "user" JOIN notifications ON "user".user_id = notifications.user_id WHERE "user".user_id=$1`
Sign up to request clarification or add additional context in comments.

2 Comments

Ah I see. Never know this.
cheers! that fixes the initial problem. However, now I get a different error message error: column reference "user_id" is ambiguous. Is that due to my table set up or again something postgres specific? Edit: nevermind, added "user".user_id to the WHERE statement and that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.