brianc/node-postgres — Issue #55: Undefined query parameters causes crash
Author: bdunavant
Created: 2011-10-13 02:46:27+00:00
State: open

If you pass an undefined value as a parameter to a query it crashes when trying to bind. Unfortunately with a stack trace that makes it very difficult to find your error (at least without putting console.logs inside of _pulseQuery to figure out which query is running).

Not sure what the correct solution would be here. Treat it as NULL? Throw an error?

Test case:

var pg = require('pg');
var conString = "tcp://user:pass@localhost/postgres";
pg.connect( conString, function(err, client) {
client.query("select $1 as foo", [ undefined ], function(err, result) {
console.log(result.rows[0].foo);
});
});

Crash output:

TypeError: Cannot call method 'toString' of undefined
at [object Object].bind (/Users/brian/node_modules/pg/lib/connection.js:138:17)
at [object Object].prepare (/Users/brian/node_modules/pg/lib/query.js:136:14)
at [object Object].submit (/Users/brian/node_modules/pg/lib/query.js:95:10)
at [object Object]._pulseQueryQueue (/Users/brian/node_modules/pg/lib/client.js:140:24)
at [object Object].query (/Users/brian/node_modules/pg/lib/client.js:164:8)
at assign_pending_items (/Users/brian/lucre/app.js:291:8)
at /Users/brian/lucre/app.js:213:3
at callbacks (/Users/brian/node_modules/express/lib/router/index.js:272:11)
at requireLogin (/Users/brian/lucre/app.js:46:3)
at callbacks (/Users/brian/node_modules/express/lib/router/index.js:272:11)