[英]NestJS can't resolve dependency of imported module
I have four modules, App -> Dog -> Access -> Mongo (dependencies as shown by the arrows).我有四个模块,App -> Dog -> Access -> Mongo(依赖项如箭头所示)。 In the App also the MongoDB is connected with
MongooseModule.forRootAsync()
which is then used in the MongoModule
with MongooseModule.forFeature()
.在应用程序中,MongoDB 也与
MongooseModule.forRootAsync()
连接,然后在MongoModule
使用MongooseModule.forFeature()
。 The full code can be found in this example project .完整代码可以在这个示例项目中找到。
I want to use the AccessGuard
provided via the AccessModule
(which internally uses the MongoModule
) to be available in the DogModule
.我想用
AccessGuard
通过提供AccessModule
(其内部使用MongoModule
)是在现有的DogModule
。
With the code below I get this error message:使用下面的代码,我收到此错误消息:
[Nest] 31164 - 11/10/2020, 5:11:29 PM [ExceptionHandler] Nest can't resolve dependencies of the AccessGuard (?). Please make sure that the argument Mongo at index [0] is available in the DogModule context.
Potential solutions:
- If Mongo is a provider, is it part of the current DogModule?
- If Mongo is exported from a separate @Module, is that module imported within DogModule?
@Module({
imports: [ /* the Module containing Mongo */ ]
})
app.module.ts: app.module.ts:
@Module({
imports: [
MongooseModule.forRootAsync({
useFactory: async () => {
let info = mongod.getInstanceInfo()
if (!info) {
await mongod.start()
}
info = mongod.getInstanceInfo()
if (info) {
return {
uri: info.uri,
dbName: "dogsDB",
useCreateIndex: true,
promiseLibrary: global.Promise,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
} as MongooseModuleOptions
}
},
}),
DogModule,
],
})
export class AppModule {}
dog.module.ts: dog.module.ts:
@Module({
imports: [AccessModule],
providers: [AccessGuard],
controllers: [DogController],
})
export class DogModule {}
access.module.ts: access.module.ts:
@Module({
imports: [MongoModule],
providers: [AccessGuard],
exports: [AccessGuard],
})
export class AccessModule {}
access.guard.ts: access.guard.ts:
@Injectable()
export class AccessGuard implements CanActivate {
constructor(private mongo: Mongo) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
return await this.mongo.hasDog()
}
}
mongo.module.ts mongo.module.ts
@Module({
imports: [
MongooseModule.forFeature([
{
name: Dog.name,
schema: DogSchema,
},
]),
],
providers: [Mongo],
exports: [Mongo],
})
export class MongoModule {}
When I add the MongoModule
to the DogModule
it works.当我添加
MongoModule
到DogModule
它的工作原理。 However, that is not how it should be I guess, because the DogModule doesn't actually use the Mongo
class.然而,我猜这不应该是这样,因为 DogModule 实际上并不使用
Mongo
类。 The DogModule
only needs the AccessGuard
from the AccessModule
. DogModule
只需要AccessModule
的AccessGuard
。
Quick note: guards and other enhancers don't need to be added to the providers
array.快速说明:守卫和其他增强器不需要添加到
providers
数组中。 They are @Injectable()
, but they live in a pseudo-space that allows them to be outside of the providers
arrays of @Module()
s.它们是
@Injectable()
,但它们存在于一个伪空间中,允许它们位于@Module()
s 的providers
数组之外。 Weird, I know, but it's how things are.奇怪,我知道,但事情就是这样。
All the needs to happen from there is if the guard is used in a module, then that module needs to have access to the guards dependencies, in this case, the module needs to have imports: [MongoModule]
as MongoModule
both provides
and exports
the Mongo
@Injectable()
class从那里发生的所有需要是,如果在模块中使用了守卫,那么该模块需要访问守卫依赖项,在这种情况下,模块需要
imports: [MongoModule]
作为MongoModule
provides
和exports
Mongo
@Injectable()
类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.