#Injecting services into MongooseModule.forFeatureAsync not triggering the mongoose middleware hooks

40 messages · Page 1 of 1 (latest)

untold mist
#

Hi everyone ! I have an issue when it comes to inject services inside a useFactory function within the MongooseModule.forFeatureAsync().

When I add the service injection, the pre-save hook won't be triggered. However, if I remove the service injection, the hook will be triggered as expected. Is there a way to inject services in useFactory function that I'm not seeing?

I've posted this question on stackoverflow, please if you can help me, follow this link: https://stackoverflow.com/questions/77732765/injecting-services-into-mongoosemodule-forfeatureasync-not-triggering-the-middle

Here you have a link to the minimum reproduction code so you can test it yourselves: https://github.com/valtervalik/service-injection-issue

GitHub

Contribute to valtervalik/service-injection-issue development by creating an account on GitHub.

unreal wind
#

Do you have a reproduction of this? I'm not super familiar with mongo myself, but I'd like to help debug this

versed depot
#

You can put the hook in the schema file instead of the module

unreal wind
#

I don't have a mongo integration setup locally. Can you provide a minimum reproduction?

untold mist
untold mist
untold mist
unreal wind
#

Using the imports and inject works when there's minimal code here. I removed all extraneous functions to make sure. There's something wrong with your setup and configuration, and there's a lot here to try to work through, so more than likely, there's something circular going on where an incorrect UserModel is being injected somewhere

#

And I think I found it. By having MongooseModule.forFeature([{ name: User.name, schema: UserSchema },]) in both UsersModule and AuthModule, the injection token for @InjectModel(User.name) gets overwritten and the new token doesn't seem to have the same schema.pre.

What I would suggest doing is putting the explicit creation in one module, and having that module export the MongooseModule, then import thwe module the exports it werever you need the UserModel so that you re-use the same model everywhere you need it

untold mist
untold mist
unreal wind
#

What I asked for was a minimum reproduction. You got me something that worked, but not a minimum set of code

untold mist
untold mist
#

It seems that the circular dependency is between the user.schema and the base.schema, apparently because the user.schema extends the base.schema

#

but that's not the issue here

#

I'm running madge to search for circular dependencies and beside ths one in the base.schema, there isn't other circular dependency. I removed the circular dependency in the baseSchema and still not working.

unreal wind
#

I bet that the issue is that there are multiple MongooseModule.forFeature calls (async or not) that create the injection token for User.name. nest sees the token already exists, and to save time doesn't see the need to create it again

#

This is why I suggested putting it in one location and exporting the mongoose module from there

untold mist
#

let me ask you. After you removed almost all the code. It actually worked for you this way?

MongooseModule.forFeatureAsync([
{
name: User.name,
useFactory: (hashingService: HashingService) => {
//This code works as expected but...
//Instead of create a new instance, I rather inject the service with the inject array
// const hashingService = new BcryptService();
const schema = UserSchema;

      schema.pre<UserDocument>('save', async function (next: NextFunction) {
        const doc = this;
        if (doc) {
          doc.password = await hashingService.hash(doc.password);
        }
        next();
      });
      return schema;
    },
    imports: [HashingModule],
    inject: [HashingService],
  },
]),
unreal wind
#

Yes

untold mist
#

did you change anything in the HashingModule or Services?

unreal wind
#

No

untold mist
#

ok thanks, i'm deleting almost everything but I can't get it working yet

unreal wind
#

When I'm back at my computer I can fork your repo and make a PR to show what differences I have

untold mist
#

thanks for helping me on this

untold mist
untold mist
#

I just saw your forked code, but you deleted the MongooseModule.forFeatureAsync(), the part of the code I wanted to actually work

unreal wind
#

I did not. I moved it to AuthModule

untold mist
untold mist
#

You really did a great job debugging the code

#

I'll add the solved tag.

inner bane
#

Do you understand what the root of the problem was?

untold mist
untold mist
#

Well, finally I did it

#

It's like @unreal wind said, I really should only use one MongooseModule.forFeature per Model