I've created the following class controller.
class ProjectsController {
queryBus: QueryBus;
commandBus: CommandBus;
constructor(queryBus, commandBus) {
this.queryBus = queryBus;
this.commandBus = commandBus;
}
async get(req, reply) {
reply.code(200).send(await this.queryBus.execute<GetOneProjectQuery, String>(GetOneProjectQuery));
}
And this is my registered route.
export default async (fastify, opts, done) => {
const { commandBus, queryBus } = fastify.di;
const projectsController = new ProjectsController(queryBus, commandBus);
await fastify.get("/", await projectsController.get);
done();
};
When I make a request, my this.queryBus
is always undefined. I don't know how to inject two objects into a class so then I can use them in a get
method. Of course, when I create
await fastify.get("/", async(req, reply)=>{this.queryBus...});
it works correctly.
Could you tell me please, if it's possible to do it in an object-oriented way?