簡體   English   中英

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 arrays 中。

您可以從文檔頁面了解有關閱讀和剖析錯誤消息的更多信息。

有一些陷阱,很常見。 一種是將提供者放入imports數組中。 如果是這種情況,錯誤將包含提供程序的名稱,其中<module>應該是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM