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

use*_*761 3 nestjs

Nest 无法解析 AuthService (?) 的依赖项。请确保索引 [0] 处的参数在 AuthModule 上下文中可用。+134ms 错误:Nest 无法解析 AuthService (?) 的依赖关系。请确保索引 [0] 处的参数在 AuthModule 上下文中可用。

我从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);
  }
}
Run Code Online (Sandbox Code Playgroud)
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;
  }
}
Run Code Online (Sandbox Code Playgroud)
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 {}

Run Code Online (Sandbox Code Playgroud)
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();
  }
}
Run Code Online (Sandbox Code Playgroud)
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();
  }
}

Run Code Online (Sandbox Code Playgroud)
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 {}

Run Code Online (Sandbox Code Playgroud)

You*_*han 5

UsersModule在您的问题中添加代码。

通常,您需要UsersServiceUsersModule