Search code examples
validationtypescriptaurelia

Inner Error: Message: controllerFactory.createForCurrentScope is not a function


I am recieving the above error in the aurelia view model

Inner Error: Message: controllerFactory.createForCurrentScope is not a function

Here is the code,

export class UpdateClient {
    public httpClient: HttpClient;
    public router: Router;
    public clientHelper: ClientHelper;
    public validator: Validator;
    public canSave: boolean;
    public controller: ValidationController;    
    public client: Client ;

 constructor(httpClient: HttpClient, router: Router, clientValidator: ClientValidator, clientHelper: ClientHelper, controllerFactory: ValidationControllerFactory, validator: Validator) {
        this.httpClient = httpClient;
        this.clientHelper = clientHelper;
        this.router = router;
        this.client = new Client
        this.controller = controllerFactory.createForCurrentScope(validator);
        clientValidator.validate(this.client, clientHelper);
}

Solution

  • You need to have @autoinject() or any other decorator on your UpdateClient class for the type metadata to be emitted correctly.

    ValidationControllerFactory is registered with DI as a resolver in the module file, so simply importing that (which you have, otherwise you'd get a type error) should give you the correct thing.

    You may need to double check that you have experimentalDecorators and emitDecoratorMetadata set to true in your tsconfig.json.

    Also make sure you are registering the validation plugin in your main.ts like so:

    aurelia.use.plugin(PLATFORM.moduleName('aurelia-validation'))