I'm having an issue setting up a pre-save hook on my User model. Here's the code snippet from my users.module.ts
:
@Module({
controllers: [
UsersController,
],
exports: [
UsersService,
],
providers: [
UsersService,
],
imports: [
forwardRef(() => AuthModule),
MongooseModule.forFeatureAsync([
{
name: User.name,
useFactory: (usersService: UsersService) => {
const schema = UserSchema;
schema.pre('save', async function(next) {
// ... more code
next();
});
return schema;
},
inject: [UsersService],
}
]),
]
})
export class UsersModule {}
An error message is showing up, saying:
Nest can't resolve dependencies of the UserModel (DatabaseConnection, ?). Please make sure that the argument UsersService at index [1] is available in the MongooseModule context.
Potential solutions:
- Is MongooseModule a valid NestJS module?
- If UsersService is a provider, is it part of the current MongooseModule?
- If UsersService is exported from a separate @Module, is that module imported within MongooseModule? @Module({ imports: [ /* the Module containing UsersService */ ] })
Error: Nest can't resolve dependencies of the UserModel (DatabaseConnection, ?). Please make sure that the argument UsersService at index [1] is available in the MongooseModule context.
The users service, with some parts omitted, looks like this:
@Injectable()
export class UsersService {
constructor(
@InjectModel(User.name)
private readonly userModel: Model<User>,
) {}
}
When I replace forFeatureAsync
with forFeature
, everything works fine. Similarly, if I remove inject: [UsersService]
and the parameter from useFactory, it also works as expected.
Any ideas on what might be causing this issue?