Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
douglasfdev opened this issue Mar 17, 2024 · 0 comments
Closed

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

douglasfdev opened this issue Mar 17, 2024 · 0 comments

Comments

@douglasfdev
Copy link

douglasfdev commented Mar 17, 2024

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
@oakserver oakserver locked and limited conversation to collaborators Mar 17, 2024
@kitsonk kitsonk converted this issue into discussion #645 Mar 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant