[英]Nest can't resolve dependencies of the CommonModule
I'm new in NEST.js world, and I trying to create simple middleware.我是 NEST.js 世界的新手,我正在尝试创建简单的中间件。 First, I created a middleware with this command:
首先,我用这个命令创建了一个中间件:
nest g middleware common/middleware/logging
And after I add my code在我添加我的代码之后
import { Injectable, NestMiddleware } from '@nestjs/common';
@Injectable()
export class LoggingMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
console.time('Request-response time');
console.log('Hi from middleware!');
res.on('finish', () => console.timeEnd('Request-response time'));
next();
}
}
And finally, I add the middleware最后,我添加中间件
import { Module, MiddlewareConsumer } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { ApiKeyGuard } from './guards/api-key.guard';
import { ConfigModule } from '@nestjs/config';
import { LoggingMiddleware } from './middleware/logging.middleware';
@Module({
imports: [
ConfigModule
],
providers: [
{
provide: APP_GUARD,
useClass: ApiKeyGuard
}
]
})
export class CommonModule {
constructor(consumer: MiddlewareConsumer) {
consumer.apply(LoggingMiddleware).forRoutes('*')
}
}
But when i try to run it:但是当我尝试运行它时:
Nest can't resolve dependencies of the CommonModule (?).
Nest 无法解析 CommonModule (?) 的依赖关系。 Please make sure that the argument Object at index [0] is available in the CommonModule context.
请确保索引 [0] 处的参数 Object 在 CommonModule 上下文中可用。
Potential solutions:
可能的解决方案:
- If Object is a provider, is it part of the current CommonModule?
如果 Object 是一个提供者,它是否是当前 CommonModule 的一部分?
- If Object is exported from a separate @Module, is that module imported within CommonModule?
如果 Object 是从单独的 @Module 导出的,那么该模块是否导入到 CommonModule 中? @Module({ imports: [ /* the Module containing Object */ ] }) +2ms Error: Nest can't resolve dependencies of the CommonModule (?).
@Module({ 导入:[ /* 包含对象的模块 */ ] }) +2ms 错误:Nest 无法解析 CommonModule (?) 的依赖关系。 Please make sure that the argument Object at index [0] is available in the CommonModule context.
请确保索引 [0] 处的参数 Object 在 CommonModule 上下文中可用。
Potential solutions:
可能的解决方案:
- If Object is a provider, is it part of the current CommonModule?
如果 Object 是一个提供者,它是否是当前 CommonModule 的一部分?
- If Object is exported from a separate @Module, is that module imported within CommonModule?
如果 Object 是从单独的 @Module 导出的,那么该模块是否导入到 CommonModule 中? @Module({ imports: [ /* the Module containing Object */ ] })
@Module({ 导入:[ /* 包含对象的模块 */ ] })
Can you help me?你能帮助我吗?
The MiddlewareConsumer
isn't a part of the constructor
. MiddlewareConsumer
不是constructor
的一部分。 Rather, your module class should implement NestModule
and should have a configure
method that takes in the consumer: MiddlewareConsumer
as the first and only paramter.相反,你的模块类应该实现
NestModule
并且应该有一个configure
方法,它接受consumer: MiddlewareConsumer
作为第一个也是唯一的参数。
@Module({
imports: [
ConfigModule
],
providers: [
{
provide: APP_GUARD,
useClass: ApiKeyGuard
}
]
})
export class CommonModule implmenets NestModule {
configure(consumer: MidlewareConsumer) {
consumer.apply(LoggingMiddleware).forRoutes('*')
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.