Skip to content

Controllers, why doesn't possible inject with private? #644

@douglasfdev

Description

@douglasfdev
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

  • Windows 11
  • deno 1.41.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions