I'm trying to find a way to get a function requiring a parameter without actually calling the function.
Current broken code example:
const _validations = {
userID: (req) => check('userID').isString().isUUID('4').run(req),
};
async function (req, res, next) {
const allReqFields = { ...req.body, ...req.params, ...req.query };
const fieldsFound = [];
for (let key of Object.keys(allReqFields)) {
if (_.has(_validations, key)) {
fieldsFound.push(_validations[key](req));
}
}
await Promise.all(fieldsFound);
return next();
},
function (req, res, next) {
const errors = validationResult(req);
if (errors.isEmpty()) {
//ALWAYS HITS HERE
return next();
}else{}
}
Code example that works but I don't want:
const _validations = {
userID: (req) => check('userID').isString().isUUID('4').run(req),
};
async function (req, res, next) {
const allReqFields = { ...req.body, ...req.params, ...req.query };
for (let key of Object.keys(allReqFields)) {
if (_.has(_validations, key)) {
await _validations[key](req);
}
}
return next();
},
function (req, res, next) {
const errors = validationResult(req);
if (errors.isEmpty()) {
//Correctly hits here
return next();
}else{
//Correctly hits here
}
}
For some context:
The function (req) => check('userID').isString().isUUID('4').run(req), returns a promise.
There are going to be more keys/values inside the _validations object.
Every function inside of _validations will require req to be passed to it.
The end goal is to run all those async functions while having req passed to them.
The issue I'm having now is that fieldsFound.push(_validations[key](req)); is pushing a promise and not the function for me to call later. Which means that fieldsFound is an array of unresolved promises
Any ideas? Thanks!
const _validations = {
userID: () => check('userID').isString().isUUID('4'),
};
Then you push the functions without calling them:
fieldsFound.push(_validations[key]);
And then at the end just call each one with req and await the promises returned:
await Promise.all(fieldsFound.map(func => func().run(req));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With