SQS 队列实现遇到错误。我正在关注以下文档:https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_sqs_code_examples.html,但我在将消息发送到时遇到类型不匹配错误队列。我附上包信息和错误快照以供参考。
Package:@aws-sdk/client-sqs
package version: ^3.379.1
import {SendMessageCommand} from '@aws-sdk/client-sqs';
import {AnyObject} from '@loopback/repository';
import {getSQSclient} from './sqs-client';
const MAX_RETRIES = 3;
let currentAttempt = 1;
export async function publishToSQS(imageInfo: AnyObject) {
try {
// Create an SQS client using the getSQSclient function
const sqs = await getSQSclient();
// Send message to SQS queue using the SQS client
const command = new SendMessageCommand({
QueueUrl: process.env.SQS_URL,
MessageBody: JSON.stringify(imageInfo),
});
// Execute the command and capture the result
const result = await sqs.send(command);
log.info('Message sent successfully to SQS:', result);
} catch (err) {
log.error(`Error sending message to SQS (Attempt ${currentAttempt}/${MAX_RETRIES}):`, err);
if (currentAttempt < MAX_RETRIES) {
currentAttempt++;
await publishToSQS(imageInfo);
} else {
// Log an error if maximum retries reached without success
log.error('All retry attempts failed. Message not sent.', {failedImageInfo: imageInfo});
}
}
我想将消息发送到
SQS queue
,但它给了我一个错误。我无法发送消息。
error TS2345: Argument of type 'SendMessageCommand' is not assignable to parameter of type 'Command<ServiceInputTypes, SendMessageCommandInput, ServiceOutputTypes, SendMessageCommandOutput, SmithyResolvedConfiguration<...>>'. Types of property 'resolveMiddleware' are incompatible. Type '(clientStack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>, configuration: SQSClientResolvedConfig, options?: HttpHandlerOptions | undefined) => Handler<...>' is not assignable to type '(stack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>, configuration: SmithyResolvedConfiguration<HttpHandlerOptions>, options: any) => Handler<...>'.
我不确定该功能的不匹配是什么,但似乎有一个。为了进行比较,您可能会在示例代码库中的此示例中找到用途。请注意,如果您有 FIFO 队列,则需要一些附加属性。您不需要属性或延迟,但它们包含在示例中。
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});
var params = {
// Remove DelaySeconds parameter and value for FIFO queues
DelaySeconds: 10,
MessageAttributes: {
"Title": {
DataType: "String",
StringValue: "The Whistler"
},
"Author": {
DataType: "String",
StringValue: "John Grisham"
},
"WeeksOn": {
DataType: "Number",
StringValue: "6"
}
},
MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.",
// MessageDeduplicationId: "TheWhistler", // Required for FIFO queues
// MessageGroupId: "Group1", // Required for FIFO queues
QueueUrl: "YOUR_SQS_QUEUE_URL"
};
sqs.sendMessage(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.MessageId);
}
});
您可以克隆此存储库并自己运行示例,这在追踪不匹配时非常有用。