Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nestjs] How can I use this inside mapProfile method in some profile class #264

Closed
micalevisk opened this issue Feb 22, 2021 · 5 comments
Closed

Comments

@micalevisk
Copy link
Contributor

micalevisk commented Feb 22, 2021

Hey!

Let's say I've this profile:

@Injectable()
export class UserProfile extends AutomapperProfile {
  constructor(@InjectMapper() mapper: Mapper) {
    super(mapper)
  }

  mapProfile() {
    return (mapper) => {
      mapper.createMap(User, UserDto)
    }
  }
}

Since I can inject dependencies via constructor like:

  constructor(@InjectMapper() mapper: Mapper, @Inject(token) appConfig: AppConfig) {
    super(mapper)
    console.assert(this.appConfig !== undefined) // ✅
  }

What is the proper way (if any) to use my this.appConfig prop in mapProfile while creating mappings? (AKA how to create mappings dynamically(?))

I'm trying to create a mapping only if it is in development mode (this.appConfig.isDev), but inside mapProfile method, this.appConfig is undefined 😕

  mapProfile() {
    return (mapper) => {
      console.assert(this.appConfig !== undefined) // ❌
      mapper.createMap(User, UserDto)
    }
  }

I understand that mapProfile is called by AutomapperProfile (when doing super(mapper))

@nartc
Copy link
Owner

nartc commented Feb 22, 2021

You can assign it to a variable in mapProfile.

mapProfile() {
   const appConfig = this.appConfig;
   return mapper => {
      console.assert(appConfig !== undefined)
   }
}

@micalevisk
Copy link
Contributor Author

I'm still getting undefined

I've reproduced this here

nartc added a commit that referenced this issue Feb 22, 2021
delay the addProfile operation for the DI to resolve all dependencies of Profile classes

#264
@nartc
Copy link
Owner

nartc commented Feb 22, 2021

@micalevisk Check out 3.0.7
I have to delay the addProfile() call to the micro task queue. Tbh, I'm not sure if it affects anything :) So please give it a try and lemme know. Thanks

@micalevisk
Copy link
Contributor Author

micalevisk commented Feb 22, 2021

It works now, ty :)


btw you could use process.nextTick as well

@nartc
Copy link
Owner

nartc commented Feb 22, 2021

@micalevisk feel free to submit a PR to change it :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants