nestjs - 如何在 NestJS 中将全局提供程序导入另一个全局提供程序?
问题描述
我想将文档中的配置提供程序导入另一个提供程序。
我正在使用此处文档中播种的相同配置结构。
所以config.module.ts
看起来像这样:
import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';
@Global()
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(
`config/env/${process.env.NODE_ENV || 'development'}.env`,
),
},
],
exports: [ConfigService],
})
export class ConfigModule {}
另一个提供者应该看起来像这样正确吗?
token.module.ts
import { Module, Global } from '@nestjs/common';
import { TokenService} from './token.service';
import { ConfigService } from './config.service';
@Global()
@Module({
import: [ConfigService]
providers: [TokenService],
exports: [TokenService],
})
export class TokenModule {}
虽然 TokenService 应该看起来像这样
token.service.ts
import { ConfigService } from '../../config/config.service';
export class TokenService {
constructor(private readonly configService: ConfigService) {}
test(): string {
return this.configService.get('TestVal');
}
}
但是当我在 AppModule 中导入 TokenModule 时出现此错误The "path" argument must be one of type string, Buffer, or URL. Received type undefined
解决方案
您忘记将您的标记TokenService
为@Injectable()
. 你需要告诉 NestJS 需要提供给其他服务的参数。
推荐阅读
- go - 如何在 go 模型中包装 proto 消息
- ckeditor - 将粘贴的剪贴板图像从 CKeditor 传递到 Dropzone
- angular - Firebase webhook 成功时返回错误
- kotlin - 从方法返回对是否被认为是 kotlin 中的一种好习惯?
- c# - nuget 没有使用 nuget 包中为 Microsoft.Extensions.Logging 指定的内容解决依赖关系
- python - 在 3d 投影上使用 plt.clim 设置间隔颜色表面
- nlog - 如何使用NLog根据参数写入不同的文件
- ios - 使用 AVAudioPlayer 在 iOS 中自动平移
- scala - 如何使用 scala 案例类作为函数参数来指定要使用的不同字段?
- html - 将 3 个 div 浮动在另一个的顶部