NestJS 导入模块时出现错误,尝试实现 JWT。

huangapple go评论71阅读模式
英文:

NestJS Error while importing a module while trying to implement JWT

问题

以下是您提供的内容的中文翻译:

我正在学习Nest,想要与mongoose一起实现JWT。我一直在按照Nestjs网站上的指南进行操作,但是一直出现以下错误:

Nest 无法解析 UsersService 的依赖项 (?). 请确保在 UsersModule 上下文中提供了索引 [0] 处的参数 User。

这是我的代码。

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: path.resolve(__dirname, '../.env'),
    }),
    MongooseModule.forRoot(process.env.MONGODB_URI),
    AuthModule,
    UsersModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(JwtMiddleware).forRoutes('*');
  }
}

auth.module.ts

@Module({
  imports: [
    UsersModule,
    JwtModule.register({
      global: true,
      secret: process.env.JWT_SECRET,
      signOptions: { expiresIn: '30d' },
    }),
  ],
  controllers: [AuthController],
  providers: [AuthService],
  exports: [AuthService],
})
export class AuthModule {}

users.module.ts

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

更新

user.service.ts

@Injectable()
export class UsersService {
  constructor(
    @Inject(User.name)
    private userModel: Model<IUser>,
  ) {}

  async findOne(email: string): Promise<IUser | undefined> {
    const user = this.userModel.findOne({ email: email.toLowerCase() });

    return user;
  }
}

请注意,由于代码包含HTML转义字符(例如&lt;&gt;),所以它们已被保留在翻译中。如果需要更多帮助,请随时告诉我。

英文:

I am learning Nest and I want to implement JWT along with mongoose. I was following the guide on nestjs website but i keep getting the error:

Nest can&#39;t resolve dependencies of the UsersService (?). Please make sure that the argument User at index [0] is available in the UsersModule context.

Here is my code.

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: path.resolve(__dirname, &#39;../.env&#39;),
    }),
    MongooseModule.forRoot(process.env.MONGODB_URI),
    AuthModule,
    UsersModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(JwtMiddleware).forRoutes(&#39;*&#39;);
  }
}

auth.module.ts

@Module({
  imports: [
    UsersModule,
    JwtModule.register({
      global: true,
      secret: process.env.JWT_SECRET,
      signOptions: { expiresIn: &#39;30d&#39; },
    }),
  ],
  controllers: [AuthController],
  providers: [AuthService],
  exports: [AuthService],
})
export class AuthModule {}

users.module.ts

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

UPDATE

user.service.ts

@Injectable()
export class UsersService {
  constructor(
    @Inject(User.name)
    private userModel: Model&lt;IUser&gt;,
  ) {}

  async findOne(email: string): Promise&lt;IUser | undefined&gt; {
    const user = this.userModel.findOne({ email: email.toLowerCase() });

    return user;
  }
}

答案1

得分: 1

Change @Inject(User.name) to @InjectModel(User.name) like the docs show

英文:

Change @Inject(User.name) to @InjectModel(User.name) like the docs show

huangapple
  • 本文由 发表于 2023年6月5日 03:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定