Basically I'm running into this error when trying to use UseCase
class on Deno.
[uncaught application error]: TypeError - Cannot read properties of undefined (reading 'getAppInfosUseCase')
request: { url: "http://127.0.0.1:3030/", method: "GET", hasBody: false }
response: { status: 200, type: undefined, hasBody: false, writable: true }
at handle (file:///C:/Users/joaop/Documents/dev/web/back-end/deno/shynotes-api%20(MVC)/src/useCases/GetAppInfos/GetAppInfosController.ts:11:39)
at dispatch (https://deno.land/x/oak@v10.6.0/middleware.ts:41:13)
at https://deno.land/x/oak@v10.6.0/router.ts:1148:20
at dispatch (https://deno.land/x/oak@v10.6.0/middleware.ts:41:13)
at composedMiddleware (https://deno.land/x/oak@v10.6.0/middleware.ts:44:12)
at dispatch (https://deno.land/x/oak@v10.6.0/router.ts:1154:28)
at dispatch (https://deno.land/x/oak@v10.6.0/middleware.ts:41:13)
at composedMiddleware (https://deno.land/x/oak@v10.6.0/middleware.ts:44:12)
at Application.#handleRequest (https://deno.land/x/oak@v10.6.0/application.ts:389:34)
at Application.listen (https://deno.land/x/oak@v10.6.0/application.ts:559:28)
App Structure
src/
app.ts
import { Application } from 'https://deno.land/x/oak@v10.6.0/mod.ts';
import { router } from './routes.ts';
import 'https://deno.land/x/dotenv@v3.2.0/load.ts';
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
export { app };
routes.ts
import { Router } from 'https://deno.land/x/oak@v10.6.0/router.ts';
import { getAppInfosController } from './useCases/GetAppInfos/index.ts';
const router = new Router();
router.get('/', getAppInfosController.handle);
export { router };
server.ts
import { app } from './app.ts';
await app.listen({
port: 3030,
});
src/useCases/
GetAppInfosController.ts
import { Context } from 'https://deno.land/x/oak@v10.6.0/context.ts';
import { GetAppInfosUseCase } from './GetAppInfosUseCase.ts';
export class GetAppInfosController {
constructor(
private getAppInfosUseCase: GetAppInfosUseCase,
) {}
handle(context: Context) {
context.response.status = 200;
return context.response.body = this.getAppInfosUseCase.execute();
}
}
GetAppInfosUseCase.ts
export class GetAppInfosUseCase {
execute() {
return {
author: 'Jphn',
github: 'github.com/Jphn',
};
}
}
index.ts
import { GetAppInfosUseCase } from './GetAppInfosUseCase.ts';
import { GetAppInfosController } from './GetAppInfosController.ts';
const getAppInfosUseCase = new GetAppInfosUseCase();
const getAppInfosController = new GetAppInfosController(getAppInfosUseCase);
export { getAppInfosController, getAppInfosUseCase };