#It's me again. Questions about injecting service..a rare scenario maybe

11 messages · Page 1 of 1 (latest)

wet hearth
#

Can I have a class is doing api stuff, for eg user.api.ts which responsible for calling API

user.api.ts wants to use a helper class user.helper.ts with @Injectable() decorator, should my user.api.ts decorate with @Injectable() too?

I found out if user.api.ts is not with the decorator and trying to inject user.helper.ts, the helper would be undefined in user.api.ts, why?

More context:
user.api.ts

export class UserApi {

}

^ is this wrong? should I decorate it with @Injectable() as well? why? I am not planning to export this class to other modules, it's for internal usage within the modules and it's trying to use some functions from user.helper.ts

user.helper.ts

@Injectable()
export class UserApi {

}

I hope I could get some helps cause I really try to ask a good question in well format, thanks..

fickle mist
#

are your user.api.ts and user.helper.ts in providers array or controllers array of any module?

gaunt pilot
#

Your .api class needs to have @Injectable()

fickle mist
wet hearth
wet hearth
#

it works this way but why, any framework knowledge behind this?

gaunt pilot
#

There's a PR on the docs repo (nestjs/docs.nestjs.com) on "Advanced Topics" that explains this

wet hearth
#
fickle mist