nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

RabbitSubscribe method is loading before my constructor

Open laxmaneasecommerce opened this issue 1 year ago • 3 comments

I am using below service to consume my queue

import { Controller } from '@nestjs/common';
import {
  RabbitSubscribe, defaultNackErrorHandler,
} from '@golevelup/nestjs-rabbitmq';
import { WebhookRabbitMqListener } from 'commerce-library';
import { QueueService } from './service/queue.service';

@Controller()
export class QueueMessageConsumer {

  constructor(private readonly warehouseService: QueueService
  ) { }
  @RabbitSubscribe({
    queue: 'V2webhook_queue',
    queueOptions: {
      durable: true
    },
    errorHandler: defaultNackErrorHandler,
  })
  async webhookV2EventsQueue({ data }: { data: WebhookRabbitMqListener }): Promise<any> {
    try {

      await this.warehouseService.processWebhookMessageQueue(data, 'V2webhook_queue')
    } catch (error) {
      console.log(error);
    }
  }
}

I am getting warehouseService is undefine in

the webhookV2EventsQueue

please help me I am stuck

laxmaneasecommerce avatar Dec 25 '24 09:12 laxmaneasecommerce

Can you try defining the webhookv2EventsQueue as an arrow function instead? Eg:

webhookv2EventsQueue = async (data) => {}

You might be losing the proper this context which would prevent you from accessing other methods or properties on the parent class.

WonderPanda avatar Dec 27 '24 15:12 WonderPanda

@laxmaneasecommerce Are you able to share more information of your dependencies such as Nestjs Version, @golevelup/rabbitmq package version, nodejs etc? Also including a stack trace would be ideal because the code you've pasted works just fine when I try to reproduce it locally

underfisk avatar Jan 12 '25 13:01 underfisk

Same here. Services from constructor aren't injected yet when @RabbitSubscribe method triggers. Node 24.

"dependencies": { "@automapper/classes": "^8.8.1", "@automapper/core": "^8.8.1", "@automapper/nestjs": "^8.8.1", "@golevelup/nestjs-rabbitmq": "^6.0.2", "@nestjs/axios": "^4.0.1", "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.1.6", "@nestjs/mongoose": "^11.0.3", "@nestjs/platform-express": "^11.1.6", "@nestjs/platform-socket.io": "^11.1.6", "@nestjs/schedule": "^6.0.0", "@nestjs/terminus": "^11.0.0", "@nestjs/websockets": "^11.1.6", "@s3pweb/keycloak-admin-client-cjs": "26.0.5", "amqplib": "^0.10.8", "axios": "^1.11.0", "builder-pattern": "^2.2.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.2", "cross-env": "^7.0.3", "date-fns": "^4.1.0", "jwt-decode": "^4.0.0", "lodash-es": "^4.17.21", "mongoose": "^8.17.1", "nest-winston": "^1.10.2", "openid-client": "^6.6.4", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.2", "winston": "^3.17.0", "winston-daily-rotate-file": "^5.0.0" }, "devDependencies": { "@automapper/types": "^6.3.1", "@nestjs/cli": "^11.0.10", "@nestjs/schematics": "^11.0.7", "@nestjs/testing": "^11.1.6", "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/bcrypt": "^6.0.0", "@types/express": "^5.0.3", "@types/jest": "30.0.0", "@types/lodash-es": "^4.17.12", "@types/multer": "^2.0.0", "@types/node": "24.3.0", "@types/supertest": "^6.0.3", "@typescript-eslint/eslint-plugin": "^8.39.1", "@typescript-eslint/parser": "^8.39.1", "eslint": "^9.33.0", "eslint-config-prettier": "^10.1.8", "eslint-plugin-prettier": "^5.5.4", "jest": "30.0.5", "prettier": "^3.6.2", "source-map-support": "^0.5.20", "supertest": "^7.1.4", "ts-jest": "29.4.1", "ts-loader": "^9.5.2", "ts-node": "^10.9.2", "tsconfig-paths": "4.2.0", "typescript": "^5.9.2" }, "overrides": { "@automapper/classes": { "reflect-metadata": "^0.2.2" }, "@automapper/nestjs": { "@nestjs/common": "^11.0.0", "@nestjs/core": "^11.0.0" } },

RaGreen avatar Aug 24 '25 18:08 RaGreen