TypeDI: class in injected service is not a function or service is not found

1.7k Views Asked by At

I'm using TypeDI in a Typescript ReactJS project and I need to generalize the User Repository/retrieval.

So what I did up to now is having an interface:

export interface IAdminUserService{
    getUsers():User[];
    getUser(id:string):User;
    ...
}

Have an implementation:

@Service('aws-user-service')
class AWSUserService implements IAdminUserService {
    ...various implementations for interface methods...
}

And finally, the class I'll be using, the one I should inject the other one in:

@Service('user-service')
export class AdminUserService{
    constructor(
        @Inject('aws-user-service') public service:AWSUserService
    ){
    }
}

Then when I need to actually use that stuff, I try to run the following:

const adminUserService: AdminUserService = Container.get<AdminUserService>('user-service'); // throw ServiceNotFound error

or the following:

const adminUserService: AdminUserService = Container.get<AdminUserService>(AdminUserService); // kinda good
adminUserSerivce.service.getUsers() // throws 'adminUserSerivce.service.getUsers' is not a function

I did put import 'reflect-metadata'; on top of each file using TypeDI.

So now I really don't know what to do/try to make this work. I even try to use Container.set(<id>, <class>) before using the Container.get(<class|id>) but it doesn't change anything at all. Also tried not putting strings into the @Service() decorator or even putting an object like {id:<id>} but still, nothing seems to work properly.

Am I doing something wrong? should I fix something, somewhere?

EDIT: Just tried the sample code from the TypeDI page and I'm getting the same exact error: serviceInstance.injectedService.printMessage is not a function so it might probably be something related to the library, more than my code itself.

0

There are 0 best solutions below