NestJS Axios 模块 - 无法解析依赖关系
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
数组中。
您可以了解有关阅读和剖析错误消息的更多信息from the docs page。
There are a few gotchas, that are common. One is putting a provider in an imports
array. If this is the case, the error will have the provider's name where <module>
should be.
我在使用 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
数组中。
您可以了解有关阅读和剖析错误消息的更多信息from the docs page。
There are a few gotchas, that are common. One is putting a provider in an
imports
array. If this is the case, the error will have the provider's name where<module>
should be.