我已经实现了一个 sqs-consumer 文件,用于侦听正在接收消息的 sqs 队列,并在其他文件中实现,但在执行 Consumer.js 文件时发生了一些错误,即 this.sqs.send() 不是该函数可以谁能告诉我为什么会出现这个错误
对于 sqs-consumer,我编写此代码,我已经在其他文件中接收消息的 sqs-receiver 代码,但在执行此 Consumer.js 文件期间,我遇到了一些错误
const AWS = require("aws-sdk");
const { Consumer } = require("sqs-consumer");
require("dotenv").config({ path: "./../../.env" });
//aws configuration
AWS.config.update({
secretAccessKey: "aws-secret-key",
accessKeyId: "access-key",
region: "us-east-1",
});
const sqs = new AWS.SQS({ apiVersion: "2012-11-05" });
const accountId = process.env.AWS_ACCOUNT_ID;
const sqsPath = process.env.SQS_PATH;
const queueName = process.env.EXPORTLOCATIONLIST_QUEUE_NAME;
const QueueUrl = `${sqsPath}/${accountId}/${queueName}`;
const subscribe = (QueueUrl, batchSpace, visibilityTimeout) => {
const consumer = Consumer.create({
queueUrl: QueueUrl,
batchSize: batchSpace || 1,
visibilityTimeout: visibilityTimeout || undefined,
attributeNames: ["All"],
handleMessage: async (message) => {
console.log(message);
},
sqs: sqs,
});
consumer.start()
return consumer
};
subscribe(QueueUrl);
module.exports = {
subscribe,
};
D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\errors.js:47
const sqsError = new SQSError(message);
^
SQSError: SQS receive message failed: this.sqs.send is not a function
at toSQSError (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\errors.js:47:22)
at Consumer.receiveMessage (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:190:43)
at Consumer.poll (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:144:14)
at Consumer.start (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:62:18)
at subscribe (D:\Samplify\runner_backend\task\src\listeners\queue_listener.js:32:12)
at Object.<anonymous> (D:\Samplify\runner_backend\task\src\listeners\queue_listener.js:38:1)
at Module._compile (node:internal/modules/cjs/loader:1356:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
at Module.load (node:internal/modules/cjs/loader:1197:32)
at Module._load (node:internal/modules/cjs/loader:1013:12)
Emitted 'error' event on Consumer instance at:
at Consumer.emit (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\emitter.js:29:22)
at Consumer.emitError (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:120:18)
at D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:170:18
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'TypeError',
statusCode: undefined,
retryable: undefined,
service: undefined,
fault: undefined,
time: 2024-01-15T07:44:44.810Z
}
Node.js v18.19.0
PS D:\Samplify\runner_backend\task\src\listeners> node queue_listener.js
D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\errors.js:47
const sqsError = new SQSError(message);
^
SQSError: SQS receive message failed: this.sqs.send is not a function
at toSQSError (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\errors.js:47:22)
at Consumer.receiveMessage (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:190:43)
at Consumer.poll (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:144:14)
at Consumer.start (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:62:18)
at subscribe (D:\Samplify\runner_backend\task\src\listeners\queue_listener.js:32:12)
at Object.<anonymous> (D:\Samplify\runner_backend\task\src\listeners\queue_listener.js:38:1)
at Module.load (node:internal/modules/cjs/loader:1197:32)
at Module._load (node:internal/modules/cjs/loader:1013:12)
Emitted 'error' event on Consumer instance at:
at Consumer.emit (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\emitter.js:29:22)
at Consumer.emitError (D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:120:18)
at D:\Samplify\runner_backend\task\node_modules\sqs-consumer\dist\consumer.js:170:18 {
code: 'TypeError',
statusCode: undefined,
retryable: undefined,
service: undefined,
fault: undefined,
time: 2024-01-15T07:45:05.188Z
}
please can any one tell me why this error occurs and for the information i am using nodejs:18.19.0 version and "aws-sdk": "^2.1534.0", "sqs-consumer": "^8.1.3"
此错误是因为您使用的是较新版本的 sqs-consumer 和 AWS SDK v2。由于从 2025 年 9 月开始将不再支持 v2,因此升级到 v3 是解决该问题的最佳选择。
更多信息:https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/
对于任何仍需要 AWS SDK v2 的应用程序,您将需要使用旧版本的 sqs-consumer 来解决此错误。
npm install [email protected]