【发布时间】:2019-09-12 22:53:16
【问题描述】:
我的中间件中有这段代码:
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”时出现以下错误的原因。调用函数。
TypeError: 无法读取未定义的属性“isNumber”
我尝试使用'bind'调用函数,但'undefined'错误仍然出现。
'next()' 函数是否破坏了正常功能?有没有办法正确使用“这个”。调用中间件中的函数?
【问题讨论】:
标签: javascript node.js express middleware