import { RouterContext, Row } from "@deps";
import { IPerson } from "../../interfaces/IPerson.ts";
import registerService, { RegisterService } from "../../services/register.ts";
const register: RegisterService = registerService; // THIS WORKS FINE
export class RegisterController {
registerService: RegisterService ;
constructor() {
this.registerService = registerService; // OR new RegisterService(); Doesn't works
}
public async registerPerson(ctx: RouterContext<string>, next: () => Promise<unknown>): Promise<Row> {
next();
const body = await ctx.request.body({ type: 'json' }).value;
console.log(this.registerService) // BUT THIS NOTS WORK
const register = await registerService.create(body);
if (!register.isCreated) throw new Error("Error to register person");
ctx.response.type = "json"
ctx.response.status = 201;
return ctx.response.body = register.person;
}
}
Throw this error:
[uncaught application error]: TypeError - Cannot read properties of undefined (reading 'cadastroService')
request: { url: "http://0.0.0.0:8080/cadastro", method: "POST", hasBody: true }
response: { status: 404, type: undefined, hasBody: false, writable: true }
I've already tried to inject it as a dependency, I've tried to instantiate the class before putting it in the controller, I've tried a lot of things and it didn't work