英文:
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转义字符(例如<
和>
),所以它们已被保留在翻译中。如果需要更多帮助,请随时告诉我。
英文:
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'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, '../.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 {}
UPDATE
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;
}
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论