Search code examples
javascripttypescriptnestjsamazon-sqs

TypeError: Cannot read properties of undefined (reading 'meta') SQS Listener Module Error in NestJS


I am trying to implement the @ssut/nestjs-sqs library for SQS service in my Nest.js project, but I'm encountering an error that I'm unable to resolve. The error message indicates that "No metadata found for: undefined" and points to the sqs.service.js file in the library.

[Nest] 59296  - 07/25/2023, 3:28:42 PM    WARN [SqsService] No metadata found for: undefined

/Users/avinash.anshu/Project/Josys/smithereens/node_modules/@ssut/nestjs-sqs/dist/sqs.service.js:55
            (_b = this.options.consumers) === null || _b === void 0 ? void 0 : _b.forEach((options) => {
                                                                                  ^
TypeError: Cannot read properties of undefined (reading 'meta')
    at /Users/avinash.anshu/Project/Josys/smithereens/node_modules/@ssut/nestjs-sqs/dist/sqs.service.js:64:49
    at Array.forEach (<anonymous>)
    at SqsService.<anonymous> (/Users/avinash.anshu/Project/Josys/smithereens/node_modules/@ssut/nestjs-sqs/dist/sqs.service.js:55:83)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/avinash.anshu/Project/Josys/smithereens/node_modules/@ssut/nestjs-sqs/dist/sqs.service.js:17:58)

And I have added the following code in module file

File:/src/global/sqs/sqs.module.ts

import { Module } from '@nestjs/common';
import { SqsModule } from '@ssut/nestjs-sqs';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as AWS from 'aws-sdk';
import { SQSListenerService } from './services/sqs-listener.service';

interface SqsConfig {
  consumers: any[]; // Change this based on SqsConsumerOptions from @ssut/nestjs-sqs
  producers: any[]; // Replace any with the actual type of producers
}

@Module({
  imports: [
    SqsModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService): Promise<SqsConfig> => {
        AWS.config.update({
          region: configService.get('AWS.REGION'),
          sslEnabled: false,
          accessKeyId: configService.get('AWS.access_key_id') + '',
          secretAccessKey: configService.get('aws.secret_access_key'),
        });
        return {
          consumers: [
            {
              name: 'FEATURE_QUEUE', // name of the queue
              queueUrl: configService.get('AWS.SQS.QUEUE_URL'), // the url of the queue
              region: configService.get('AWS.REGION'),
            },
          ],
          producers: [],
        };
      },
      inject: [ConfigService],
    }),
],
  providers: [SQSListenerService]
})
export class SqsListenerModule {}

Additional Information:

  • I am using TypeScript in my Nest.js project.
  • The sqs.service.js file is part of the @ssut/nestjs-sqs library, which handles the SQS integration.

I would appreciate any insights or guidance on why this error is occurring and how I can resolve it. If anyone has successfully integrated @ssut/nestjs-sqs into a Nest.js project, I would love to hear about your experience and any tips you may have.

Please let me know if there are any specific details I can provide to help diagnose the issue further.


Solution

  • You have to implement the consumer of your queue.

    @SqsMessageHandler("FEATURE_QUEUE", false)
    public async handleMessage(message: Message) {
        console.log(message);
    }
    
    @SqsConsumerEventHandler("FEATURE_QUEUE", "processing_error")
    public onProcessingError(error: Error, message: Message) {
        console.log(error, message);
    }