我尝试在app.model.ts中设置isGlobal:true,但process.env.*返回未定义。
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath,
}),
...
})
export class AppModule {}
接下来,我尝试在Strategy类中注入ConfigService,但仍然没有定义。
import { Strategy } from 'passport-auth0';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, Logger } from "@nestjs/common";
import { Request } from "express";
import { ConfigService } from "@nestjs/config";
@Injectable()
export class Auth0Strategy extends PassportStrategy(Strategy) {
private readonly logger = new Logger(Auth0Strategy.name);
constructor(private configService: ConfigService) {
super({
domain: configService.get<string>('AUTH0_DOMAIN'),
clientID: configService.get<string>('AUTH0_CLIENT_ID'),
clientSecret: configService.get<string>('AUTH0_CLIENT_SECRET'),
callbackURL: configService.get<string>('AUTH0_CALLBACK_URL'),
passReqToCallback: true
});
}
async validate(req: Request, accessToken: string, refreshToken: string, extraParams, profile, done) {
...
}
}
我是NodeJS,NestJS,Typescript的新手,。。。来自JavaSpring背景。感谢您的帮助!
在项目中添加ConfigurationModule
@Module({
imports:[ConfigModule.forRoot({
envFilePath: ['your env name'],
load:[configuration],
isGlobal: true
})],
})
export class ConfigurationModule {}
注意:配置用于数据库配置
然后在您的appmodule 中导入ConfigurationModule
注意:像这样使用process.env:
domain: process.env.AUTH0_DOMAIN
您不需要在构造函数中定义configService