Is this the correct way of chaining promises when it comes to express-validator? I am using express node and this is the endpoint for a get request
Something about it seems a bit messy, but I can't think of a cleaner way to do it.
function exitChain() {
return Promise.reject();
}
function getUserByID(req, res) {
const id = req.query.id;
req.checkQuery('id', 'ID is not a number').isInt();
const q = 'select * from users where user_id=$1';
const p = [id];
req.getValidationResult()
.then(function(vRes) {
if (!vRes.isEmpty()) {
exitChain();
return res.status(400).send('Validation Fail: ' + util.inspect(vRes.array()));
}
})
.then(function() {
pg.connect(postgresConfig)
.then(function(client) {
return client.query(q, p).execute();
})
.then(function(result) {
result.client.done();
return res.status(200).send(res.json(result.rows));
})
.catch(function(err) {
console.log(err);
return res.status(400).send('Error: ' + err);
});
})
.catch(function(err) {
console.log(err);
return res.status(400).send('Error: ' + err);
});
}