1

在 AuthService 中使用此代码:

@Injectable()
AuthService {

  constructor(
    @InjectRepository(UserRepository)
    private jwtService: JwtService
    private userRepository: UserRepository
    ) {
  }

  async login(loginCredentialsDto: LoginCredentialsDto): Promise<{ accessToken: string }> {
    const email = await this.userRepository.validatePassword(loginCredentialsDto);
    const payload: JwtPayLoad = {email};
    return {
        accessToken: this.jwtService.sign(payload),
    };
  }
}

在运行时编译后,我会收到以下错误:

[Nest] 12998 - 07/03/2020, 9:22:20 PM [ExceptionsHandler] this.jwtService.sign 不是函数 +213303ms

以下是 AuthModule:

@Module({
    imports: [
        PassportModule.register({ defaultStrategy: 'jwt' }),
        JwtModule.register({
            secret: 'topSecret51',
            signOptions: {
                expiresIn: 3600,
            }
        }),
        TypeOrmModule.forFeature([UserRepository])],
    controllers: [AuthController],
    providers: [AuthService, JwtStrategy],
    exports: [
        JwtStrategy,
        PassportModule,
    ], }) 
export class AuthModule { }

任何想法如何解决?

我重新安装了 npm,删除了节点模块并尝试修改代码。但它仍然不起作用。

4

1 回答 1

1

Authservice 构造函数中的问题应该是:

constructor(
    @InjectRepository(UserRepository)
    private userRepository: UserRepository,
    private jwtService: JwtService) {
}
于 2020-07-04T10:24:30.377 回答