【发布时间】:2021-12-07 21:04:49
【问题描述】:
我想实现 Nestjs v8.1.1 的ConfigService
我已将 .env 文件放在项目的根目录(不是 src 文件夹)中,该文件包含以下内容:
HOST=http://localhost
PORT=8088
app.Module.ts 通过导入丰富:
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
envFilePath: '.env',
}),
...]
我已经尝试不使用这些选项并使用这些列出的选项。
当它试图获取变量的值时,它会导致
var address = this.configService.get('HOST', 'http://localhost')
^
TypeError: Cannot read properties of undefined (reading 'get')
仅供参考,代码是在扩展类中运行的,这应该没什么区别。
export default class KsqldbSignalsClient extends ClientProxy {
constructor(private configService: ConfigService) {
super()
}
async connect(): Promise<any> {
var address = this.configService.get('HOST', 'http://localhost')
...
感谢任何提示(甚至确认它对您有用)。
【问题讨论】:
-
这意味着
ConfigService还没有被实例化。你用@Injectable()装饰了KsqldbSignalsClient类吗? -
@msrumon 就是这样。谢谢
-
@msrumon,请将其作为答案发布,以便我标记给其他人。
标签: nestjs nestjs-config