gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:44:03 25 4
gpt4 key购买 nike

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





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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com