Nestjs service injection into CQRS yields an undefined result

I encountered an issue with a service I'm trying to import in CQRS during runtime - specifically, when using the service method.

The service is declared in the constructor and called in the execute method as shown below:

@CommandHandler(UpdateSensorsProductsCommand)
export class UpdateSensorsProductsCommandHandler implements ICommandHandler<UpdateSensorsProductsCommand>
{
  constructor(
    private eventBus: EventBus,
    private sensorProductListService: SensorProductsListService,
  ) {}

  async execute(
    command: UpdateSensorsProductsCommand,
  ) {
    // Error message received: TypeError: Cannot read property 'getAllSensorsProducts' of undefined
    this.sensorProductListService.getAllSensorsProducts()
  }
}

In addition, I have imported the sensorProductListService module in my CQRS module named SystemCqrsModule:

@Module({
  imports: [
    CqrsModule,
    SensorProductsListModule,
  ],
    ...
  exports: [CqrsModule],
})
export class SystemCqrsModule {}

Furthermore, I export sensorProductListService from the sensorProductListModule:

This is the sensorProductListModule:

@Module({
  providers: [SensorProductsListService, UnitConvert],
  exports: [SensorProductsListService],
})
export class SensorProductsListModule {}

Despite these configurations, the following error persists:

TypeError: Cannot read property 'getAllSensorsProducts' of undefined

What could be causing this error?

Answer №1

If you have a request scoped instance within a class that is injected into your service, it won't function properly.

You can either eliminate the request scoped dependency or manage it with moduleRef. More details are available here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to wait for a button's listeners to resolve in JavaScript?

Currently, I am conducting frontend tests utilizing Jest with the jsdom environment to simulate a DOM tree and manually trigger actions such as button.click(). My goal is to be able to await button.click(), which in my expectations should wait for all of ...

Disable the loader for a specific method that was implemented in the Interceptor

Custom Loader Interceptor A loader has been implemented within an Interceptor. I have a specific requirement where the loader should not be triggered during the upload() function. It should not be applied to that particular method only. ...

Submit a pdf file created with html2pdf to an S3 bucket using form data

Currently, I have the following script: exportPDF(id) { const options = { filename: 'INV' + id + '.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2, dpi: 300, letterRendering: true, useC ...

What is the method for substituting one text with another using two-way data binding?

I implemented two different cases in my Mat-Table. When there is no data, the user will see a message saying "No Data Found". However, if the user enters text in the filter search, the "No Data Found" message should be hidden and replaced with the entered ...

Unable to activate the AWS IoT security audit using CDK

I'm trying to activate the AWS IoT security audit using a CDK stack, but I'm encountering some issues. Initially, I referred to this documentation for the interfaceAuditCheckConfigurationProperty and implemented the following CDK code to enable ...

What is the correct way to invoke a function that accepts a combination of specific string values when all I have is a regular string?

Within the TypeScript function declaration provided below, the parameter type alignment consists of unioned literals. function printText(s: string, alignment: "left" | "right" | "center") { // ... } As per the documentation ...

Angular is showing an error indicating that the property "name" is not found on an empty object

After thorough checking, I have confirmed that the property does exist with the correct key. However, it is returning an error message stating name is not a property of {}. I attempted to assign this object to an interface along with its properties but enc ...

Error in TypeScript on SendGrid API: Invalid HttpMethod

Here is my code snippet: import sendgridClient from '@sendgrid/client' sendgridClient.setApiKey(process.env.SENDGRID_API_KEY); const sendgridRequest = { method: 'PUT', url: '/v3/marketing/contacts', bo ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...

Generate an alert with a numerical input field - Ionic

How can I create an input with type number in AlertController? I attempted to implement this, but the input only accepts text and not numbers. const alert = this.alertCtrl.create({ title: 'Add Ingredient', inputs: [ { name: ' ...

connecting models with sequelize-typescript

I'm currently working on establishing the following relationships: User is associated with one Account User is linked to one Application Application has multiple Members The issue I'm encountering is that when the models are generated, the acco ...

Can I access the component attributes in Vuetify using Typescript?

For example, within a v-data-table component, the headers object contains a specific structure in the API: https://i.stack.imgur.com/4m8WA.png Is there a way to access this headers type in Typescript for reusability? Or do I have to define my own interfac ...

Is there a method in TypeScript to make an enum more dynamic by parameterizing it?

I've defined this enum in our codebase. enum EventDesc { EVENT1 = 'event 1', EVENT2 = 'event 2', EVENT3 = 'event 3' } The backend has EVENT1, EVENT2, EVENT3 as event types. On the UI, we display event 1, event 2, a ...

What causes certain webpack / Babel ES6 imports without a specified extension to resolve as "undefined"?

When I try to import certain ES6 files (such as .js, .jsx, .ts, .tsx) using the syntax import ComponentName from './folder/ComponentName'; (without extension), they end up resolving as undefined. This occurs even though there are no errors from W ...

Should I use Mongoose Schemas to enhance the Document class or & Document?

Currently, I'm working with NestJS to develop backend code and retrieve objects from MongoDB. In their guide, they showcase examples that involve creating a class with the @Schema() annotation and then combining it with their pre-built mongoose Docume ...

Data retrieved from API not displaying in Angular Material table

I've hit a roadblock trying to understand why my mat-table isn't displaying the data for me. I'm working with Angular 15 and using Angular Material 15. Below is my HTML component code: <mat-divider></mat-divider> <table mat-t ...

What is causing the undefined value for the http used in this function?

My Code Component import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; @Component({ selector: 'app-root', template: '<button id="testBtn"></button>' }) export c ...

Straightforward, rudimentary example of Typescript knockout

I am hoping to successfully implement a basic TypeScript Knockout example. I utilized Nugget to acquire the TypeScript Knockout and downloaded Knockout 3.0.o js. Below is my TypeScript file: declare var ko; class AppViewModel { firstName = ko.observa ...

The comparison between serialization at the PostgreSQL layer and the application layer

I have come across limited information and opinions on this topic. When it comes to serialization, is it better to perform it directly on the SQL layer (specific column selection) or is it acceptable for performance reasons to retrieve the entire record ( ...

Curious about where the data comes from? Look no further than TypeScript!

Can anyone help me figure out the source of my data in terms of class and method? Here's an example: EditValue(data) { /* my operations */ } I have several classes in my project that send data to this EditValue(data) method. Is there a method in T ...