Im using connect-domain to handle the errors for DB connection. When no data is found in DB, connect-domain throws the error. Below is the code
db.store.find({'_id':id},function (err, data) {
if (err) {
throw new Error("error");
}
else if (data == null) {
throw new Error("data not present in DB");
// DB connection close
}
else
{
console.log(data);
});
But I want DB connection also to be closed when such errors occur.
Any help on this will be really helpful.
Thanks,
Are you using native mongo driver for node or mongoose? If you're using the native drive, just close the connector.
var connector = new mongodb.Db(dbName, mongodb.Server('127.0.0.1', 27017));
.
.
.
connector.close();
If you're using mongoose, it'll take care of the connection for you.