nestjs - Nest 的 ConfigService 无法拉取 .env 变量
问题描述
我想实现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')
...
任何提示(甚至确认它对您有用)都值得赞赏。
解决方案
用装饰KsqldbSignalsClient
班级@Injectable()
。
推荐阅读
- ios - 在 SwiftUI 3 中交换选项卡视图指示器的配色方案
- python - 使用 Python 日志记录消息和标准错误
- json - 带 curl 的 Keycloak POST(创建用户和组)
- javascript - 使变量与对象方法相等
- windows - 如何检测正在进行的 webex 会议或 google 会议
- bash - 如何使用自定义文件夹中的所有脚本,例如 macOS 中的本机 shell 命令
- javascript - 在本地捕获 mux.monitor 对象
- python - 如何从不同表中的字段创建复合外键?
- node.js - TypeORM - 如何在不先获取关系的情况下使用外键插入表
- jupyter-notebook - 使用 sys.stdout 时 jupyter 笔记本单元未终止