2

I want to make sure this isn't a user error before creating a bug report

Using sails-postgresql adapter I have a query (I've dummied down for purposes of this question) that wants to handle a NULL value, but I get an error no matter how I write the query. I've tried different variations of it, but you should get the jist of what I want to do here:

parent = req.param('parent') or null
Route.query 'SELECT * FROM route WHERE parent '+(if not parent then 'is' else '=')+' $1', [parent], (err, routes)->
    console.log(err, routes)
    if err
        return res.json(400, [error: sails.__('Database.connect'), _error: err])
    res.json(routes.rows)

Some variation of the above where I'm always passing in data will always error out.

A simple fix is

parent = req.param('parent') or null
values = []
if(parent)
    values.unshift(parent)
Route.query 'SELECT * FROM route WHERE parent '+(if not parent then 'is NULL' else '= $1'), values, (err, routes)->

Which is just kind of a nuisance.

Is there a better way to do that? I have to assume this raw query is making a postgres prepared statement so perhaps it's how postgres is actually handling the null. I haven't dug much into the source to see what is going on yet.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108
  • If you think this is a bug, feel free to file in https://github.com/balderdashy/sails-postgresql/issues. Thanks! – Travis Webb Apr 19 '15 at 23:33
  • The sails.js gitter room is also a good place to get live community support: https://gitter.im/balderdashy/sails – Travis Webb Apr 19 '15 at 23:34

0 Answers0