我的中间件中有此代码:
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();
},
我通常使用“这个”。可以毫无问题地调用对象中的函数和变量。我怀疑中间件中的“next()”函数是导致我在使用“this”时出现以下错误的原因。调用一个函数。
类型错误:无法读取未定义的属性“isNumber”
我曾尝试使用“绑定”来调用该函数,但仍然会出现“未定义”错误。
'next()' 函数是否破坏了正常功能?有没有办法正确使用“这个”。在中间件中调用函数?
喵喵时光机
相关分类