问题描述:打印this值为undefined
import { Injectable, NestMiddleware } from '@nestjs/common';
import { CacheUtil } from 'src/utils/cache.util';
@Injectable()
export class GlobalMiddleware implements NestMiddleware {
constructor(
private readonly cacheUtil: CacheUtil
) { }
async use(req: any, res: Response, next: () => void) {
console.log(this); // undefined
console.log(this.cacheUtil); // Cannot read properties of undefined (reading 'cacheUtil')
next();
}
}
nest 中间件中遇到的问题,为什么this的值为undefined。