[英]NestJS Axios module - can't resolve dependencies
我对 NestJS Axios 模块有一个莫名其妙的问题。 我将它导入到我的模块中,如下所示:
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { PassportModule } from '@nestjs/passport';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { AuthStrategy } from './auth.strategy';
@Module({
controllers: [AuthController],
providers: [AuthService],
imports: [HttpModule, PassportModule, AuthStrategy],
})
我试图在我的AuthStrategy
中使用它,如下所示:
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { Strategy } from 'passport-oauth2';
@Injectable()
export class AuthStrategy extends PassportStrategy(Strategy) {
constructor(private readonly httpService: HttpService) {
super({
authorizationURL: `${process.env['OAUTH_DOMAIN']}/authorize`,
tokenURL: `${process.env['OAUTH_DOMAIN']}/oauth/token`,
clientID: process.env['OAUTH_CLIENT_ID'],
clientSecret: process.env['OAUTH_CLIENT_SECRET'],
callbackURL: process.env['OAUTH_REDIRECT_URL'],
scope: ['openid', 'email', 'profile'],
});
}
async validate(accessToken: string): Promise<any> {
const data = await this.httpService.get(`${process.env['OAUTH_DOMAIN']}/`, {
headers: { Authorization: `Bearer ${accessToken}` },
});
console.log(data);
}
}
但我得到了错误:
Error: Nest can't resolve dependencies of the AuthStrategy (?). Please make sure that the argument HttpService at index [0] is available in the AuthStrategy context.
我哪里错了? 我已经完成了错误提示的操作,但仍然无法正常工作
在您的应用程序的某处,您将AuthStrategy
(提供程序)添加到模块的imports
数组中。 提供者永远不应该在imports
arrays 中。
您可以从文档页面了解有关阅读和剖析错误消息的更多信息。
有一些陷阱,很常见。 一种是将提供者放入
imports
数组中。 如果是这种情况,错误将包含提供程序的名称,其中<module>
应该是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.