I have this code in my middleware:
const UserMiddleware = {
isNumber(n) { return !Number.isNaN(parseFloat(n)) && !Number.isNaN(n - 0); },
// eslint-disable-next-line consistent-return
validateSignUp(req, res, next) {
const allSignUpErrors = [];
console.log(this.isNumber(5));
if (this.isNumber(req.body.first_name)) {
allSignUpErrors.push('First name must be a text value');
}
if (allSignUpErrors.length !== 0) {
return res.status(400).json({
status: 400,
error: allSignUpErrors,
});
}
next();
},
I normally use 'this.' to call functions and variables in objects without problems. I suspect that the 'next()' function in the middleware is what is causing me to get the error below at the point of using 'this.' to call a function.
TypeError: Cannot read property 'isNumber' of undefined
I have tried using 'bind' to call the function but the 'undefined' error still occurs.
Is the 'next()' function the one breaking the normal functionality? Is there a way to correctly use 'this.' to call functions in middleware?