nestjs - Nest 无法解析 AuthService (?) 的依赖关系。请确保索引 [0] 处的参数在 AuthModule 上下文中可用

标签 nestjs

这个问题在这里已经有了答案:





Nest can't resolve dependencies of the service which imports JwtService

(1 个回答)


2年前关闭。




Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument at index [0] is available in the AuthModule context. +134ms Error: Nest can't resolve dependencies of the AuthService (?). Please make surethat the argument at index [0] is available in the AuthModule context.



我从 https://docs.nestjs.com/techniques/authentication 复制了代码,然后报这个错误:
import { Injectable, Inject } from '@nestjs/common';
import { UsersService } from '../users/users.service';
@Injectable()
export class AuthService {
  //constructor(private readonly usersService: UsersService) {}
  constructor(@Inject('UsersService') private UsersService: UsersService) {}
  async validateUser(token: string): Promise<any> {
    // Validate if token passed along with HTTP request
    // is associated with any registered account in the database
    return await this.UsersService.findOneByToken(token);
  }
}
import { Strategy } from 'passport-http-bearer';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class HttpStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super();
  }

  async validate(token: string) {
    const user = await this.authService.validateUser(token);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { HttpStrategy } from './http.strategy';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'bearer' }),
    UsersModule,
  ],
  providers: [AuthService, HttpStrategy],
})
export class AuthModule {}

import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Users } from './users.entity';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(Users)
    private readonly UsersRepository: Repository<Users>,
  ) {}

  async findAll(): Promise<Users[]> {
    return await this.UsersRepository.find();
  }

  async findOneByToken(token): Promise<Users[]> {
    return await this.UsersRepository.find();
  }
}
import { Controller, UseGuards, Get } from '@nestjs/common';
import { UsersService } from './users.service';
import { AuthGuard } from '@nestjs/passport';

@Controller('users')
export class UsersController {
  constructor(private readonly UsersService: UsersService) {}
  @Get('users')
  @UseGuards(AuthGuard())
  get() {
    return this.UsersService.findAll();
  }
}

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { Users } from './users.entity';
@Module({
  imports: [TypeOrmModule.forFeature([Users])],
  providers: [UsersService],
  controllers: [UsersController],
})
export class UsersModule {}

最佳答案

请添加UsersModule的代码在你的问题中。

一般需要导出UsersService来自 UsersModule

关于nestjs - Nest 无法解析 AuthService (?) 的依赖关系。请确保索引 [0] 处的参数在 AuthModule 上下文中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54370079/

相关文章:

typescript - 我需要帮助引用在 NestJs 的 @Schema 属性中声明的 id

postgresql - TypeORM 找不到\"MyEntity\"的元数据

node.js - Mongoose 与 NestJS - 一对多

mongoose - 在 Nest.Js 的 useFactory 中使用其他模型

node.js - 如何使用cqrs模块在nestjs中进行查询?

nestjs - 构建服务器端 i18n 的最佳方式

typescript - 将接口(interface)列表注入(inject)类 - NestJs

mysql - 如何在带有typeORM的nestjs中创建日期类型和日期时间类型的列?

jestjs - Jest在nestjs中找不到路由

swagger - 无法解决@nestjs/swagger 的依赖关系