【问题标题】:problem nestjs - Nest can't resolve dependencies of the AuthenticationService (?)问题nestjs - Nest 无法解析 AuthenticationService 的依赖项(?)
【发布时间】:2022-02-17 16:22:14
【问题描述】:

应用程序在启动时立即停止,尚未检查其他模块,因此它们不太可能出错。

authentication.service.ts

import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { CreateUserDto } from 'src/users/dto/createUser.dto';
import { UserService } from 'src/users/user/user.service';
import bcrypt from 'bcrypt';

@Injectable()
export class AuthenticationService {
    constructor(@Inject() private readonly userService: UserService){}
    public async regiser(registartionData:CreateUserDto){
    const hashingPassword = await bcrypt.hash(registartionData.passwordHash, 10);
    try{
        const createUser = await this.userService.create({
            ...registartionData,
            passwordHash: hashingPassword
        })
        createUser.passwordHash = undefined;
        return createUser;
    }
    catch(err){
        throw new HttpException('Something went wrong', HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
public async getAuthenticationUser(email:string, textPassword:string){
    try{
        const user = await this.userService.findByEmail(email);
        await this.verifyPassword(textPassword,user.passwordHash);
        user.passwordHash = undefined;
        return user;
    }
    catch(err){
        throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
    }
}
private async verifyPassword(password:string, hashingPassword:string){
    const isMatching = await bcrypt.compare(password, hashingPassword);
    if(!isMatching) throw new HttpException('Wrong credentials provided', HttpStatus.BAD_REQUEST);
}
}

auth.module.ts

import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { UsersModule } from 'src/users/users.module';
import { AuthController } from '../controllers/auth/auth.controller';
import { AuthenticationService } from './authentication/authentication.service';
import { LocalStrategy } from './authentication/local.strategy';

@Module({
  imports:[UsersModule, PassportModule, ],
  providers: [AuthenticationService, LocalStrategy],
  controllers:[AuthController],
  exports: [AuthenticationService]
})
export class AuthModule {}

这个错误

 Nest can't resolve dependencies of the AuthenticationService (?). Please make sure that the argument dependency at index [0] is available in the AuthModule context.

Potential solutions:
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

我不懂错误,导出并提供认证服务。我不在其他模块中使用身份验证服务。

【问题讨论】:

  • 在 AuthenticationService 你有:constructor(@Inject() private readonly userService: UserService){},但在 AuthModule 中没有。您确定 UsersModule 导出 UserService 吗?
  • user.module.ts import { Module } from '@nestjs/common'; import { UserService } from './user/user.service'; @Module({ providers: [UserService], exports: [UserService], }) export class UsersModule {}
  • 你为什么在没有依赖令牌的情况下使用@Inject()
  • @Inject() 不需要,我删除了它,但是**新错误** - Nest 无法解析 UserService (?) 的依赖项。请确保索引 [0] 处的参数 [object Object] 在 UsersModule 上下文中可用。
  • @JayMcDoniel 你可以使用@Inject 与否,这真的取决于你(对于简单的注射剂,你使用它没有任何东西),但这里的问题是,没有任何 UserService 导出在模块中导入的

标签: node.js typescript nestjs


【解决方案1】:

您的仓库中的代码甚至无法编译,因此几乎无法帮助您。有缺东西,ts-errors等。

但据我所知:

  • UsersModule 有提供者UserService,但它没有导出它。因此没有其他模块可以访问它。您还需要导出 UserService。
  • AuthModule 不导入 UserService(也没有导出它),因此在 Nest 尝试匹配注入时找不到它。你可以做的是添加imports: [UserModule] in AuthModule(也可以在 UserModule 中导出 UserService)。

再次阅读模块文档和示例项目,了解导入、导出、控制器和提供程序的工作原理。

【讨论】:

  • 抱歉,我可能还没有完全更新存储库。是的,我在用户模块中添加了导出,并在授权模块中导入,如手册中所述。错误仍在早期重现。f您可以再次查看存储库
【解决方案2】:

我所有的服务都使用 mongodb 模型。因此,您需要在模块和服务中进行以下操作。

user.module.ts

@Module({
*imports: [MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],*
  providers: [UserService],
  exports: [UserService],
})
export class UsersModule {}

user.service.ts

@Injectable()
export class UserService implements IUserService {
  constructor(
*@InjectModel(User.name) private userModel: Model<User>*
) {}

很好的例子https://wanago.io/2021/08/23/api-nestjs-relationships-mongodb/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 2020-08-12
    • 2023-04-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    相关资源
    最近更新 更多