Search code examples
postgresqlnode-postgres

Unable to fathom node-postgres error


Using node-postgres, the following snippet works fine:

let shift_solutions = function (ufrom, uto) {
    let cmd = 'update solutions set "user" = \''+uto+'\' where "user" = \''+ufrom+'\''
    client.query( cmd, null, function (err,rslt) { 
        ... works

but the if I change the above to:

    client.query('update solutions set "user" = %2 where "user" = %1',
                 [ufrom,uto], 
                 function (err,rslt) { 
        ... fails

yields - Unhandled rejection error: syntax error at or near "%".

I suspect it might have something to do with the fact that the user field is of type 'uuid', but don't really have a clue tbh. The parameters are supplied as strings:

[ '8e479385-5692-4acc-8dd7-4630480bd17f',
  '0cc0832e-1f01-40a9-aaa4-30ae8e56d708' ]

Anybody able to shed some light on what I am doing wrong here? Thanks.


Solution

  • Use the dollar sign for your parameters ie

    client.query('update solutions set "user" = $2 where "user" = $1'