hyprstack
hyprstack

Reputation: 4228

Nodejs - Postgres - Notify - Error bind message

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!

Upvotes: 1

Views: 316

Answers (1)

Julius Žaromskis
Julius Žaromskis

Reputation: 2255

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
}

Upvotes: 2

Related Questions