Reputation: 10166
I'm using nodeJS with the node-postgres module and i have to do some simple queries but i can't understand how can i catch errors on insert queries. That's my sample code:
pg.connect(conString, function (err, client, done) {
client.query("INSERT INTO foo(bar) VALUES ('lol')");
res.json({'err':0,'message':'successful insert'});
});
I would like to add a res.json({'err':1,'message':'ERRROR'});
in case of error during the query.
Upvotes: 1
Views: 80
Reputation: 19372
Due to example in github page: https://github.com/brianc/node-postgres
Screenshot: http://joxi.ru/K823gg6Uvx3D2O
Try this:
pg.connect(conString, function (err, client, done) {
if(err) {
console.log('Cannot connect!', err);
return;
}
client.query("INSERT INTO foo(bar) VALUES ('lol')", [], function(err, result) {
if(err) {
console.log('Error: ', err);
res.json({'err':1,'message':'ERRROR'});
return;
}
res.json({'err':0,'message':'successful insert'});
});
});
Upvotes: 2