1

I am using node-postgres (v-6.1.2)

This is code I am refactoring because of un-parameterized params in the query. This is code I did not write!

I have the following function publish, where pubClient is the result of

new pg.Client(pubsubUri).connect((err, client) => {
    if(err) return logger.error('database-connect', err.message)

    pubClient = client
})

When I run publish I get the following error:

error: bind message supplies 2 parameters, but prepared statement "" requires 0

This is my publish method: (type and message are both of type string)

let publish = ( type, message) => {
    if (pubClient) {
        pubClient.query('NOTIFY "$1", \'$2\'', [type, message], (err, res) => {
            if (err) {
                console.log(err)
              return
            }
          return true
        })

    }

    return false
}

I can't figure out what the correct syntax is for the query or why I am getting this error.

Any help would be appreciated!

5
  • How are you calling publish? Commented Apr 4, 2018 at 16:19
  • Also you're returning true and false incorrectly. This is asynchronous programming and you need to callback with true or false Commented Apr 4, 2018 at 16:19
  • 1
    I know. This is code I'm refactoring (which I did not write). It was actually being run synchronously. I simply added the callback to see what the error was Commented Apr 4, 2018 at 16:22
  • As is publish is being called as publish('HELLO', {"payload": {....}}) Commented Apr 4, 2018 at 16:24
  • It must be the query string that's messed up. It looks odd as well. Commented Apr 4, 2018 at 16:28

1 Answer 1

2
NOTIFY channel [ , payload ]

channel can not be parameterized. Try using pg_notify:

let publish = (type, message) => {
    if (pubClient) {
        pubClient.query('SELECT pg_notify($1, $2)', [type, message], (err, res) => {
            if (err) {
                console.log(err)
              return
            }
          return true
        })

    }

    return false
}
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Thanks! Where in the docs does it say channel cannot be parameterised?
It does not say in the docs. But you can see from sample query NOTIFY test, 'test'; first parameter is not a string, but rather an identifier, and thus can not be bound. Same thing happens with table names - you can't bind them. pg_listen should work.

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.