#Dependency İnjection Error
1 messages · Page 1 of 1 (latest)
Suggestion for @small dagger:
:warning: Please do not screenshot code as it causes a number of issues:
- Ease of assistance: if someone wants to copy your code and correct it, they cannot. Making it easy for people to help you is in your best interests.
- Editorializing: it's common to try to make images small, which means you're likely to crop out code relevant to your issue
- Accessibility: wide images can be hard to read on mobile devices, and are impossible for screen readers.
- Legibility: you cannot read screenshots of code directly, instead you have to open them in an enlarged context.
- Bandwidth usage/clutter: some of our members use metered connections, and it is wasteful for them to download images of a text.
For a small amount of code, please use a code-block.
looks like that error has nothing to do with dependency injection tho
double check all the @CommandHandler() occurrences and if they were added to the providers array correctly
Hi Mrs Micalevisk my code this like. I checked but I didnt
Show us the code that has the @CommandHandler for the command mentioned in the error msg
My code this like. İf you want to check my code. I send github repos https://github.com/mi-cloud34/nest-cqrs-e-commerce
Should I use categoryRepository instead of GetCategoryByIdQuery for categoryId control when adding a product?
Hi @small dagger , I looked at the repository you shared. From what I understand, you are calling an API that is mapped to createProduct method in the Product controller. For brevity
@Post()
@UseGuards(AuthenticationGuard)
@Message('Product created successfully.')
async createProduct (@Req() req, @Body() createProductDto: CreateProductDto) {
const category = await this.commandBus.execute(new GetCategoryByIdQuery(createProductDto.categoryId.toString()));
console.log("categoryIdddd",category);
if (!category) {
throw new NotFoundException(`Category with id ${category} not found`);
}
console.log("userIddddd",req.user.user._id);
return this.commandBus.execute(new CreateProductCommand(createProductDto))
}
please note that you are using command bus to execute a query, so this should have been a query bus . Also, imho after you try this change, the handler would not have been found because it was imported in another module (CategoryModule) and said module have not exported the handler (which is okay to me because you should not be knowing what category is doing in product..).
At best I'd suggest you export the CategoryService and create a method that will still invoke the query bus and execute the query