NodeJS 使用SQSClient发送消息时,AWS SQS on Lambda抛出参数必须为ArrayBuffer

pnwntuvh  于 2023-01-20  发布在  Node.js
关注(0)|答案(1)|浏览(162)

我正在尝试使用lambda节点16 TypeScript向SQS发送消息。
如果我在本地运行它,代码可以工作。但是如果我在外部运行它,我会得到这个错误:

TypeError: The "input" Received type object ([object Object])

//full message is actually part of the response body: 
//"{\"message\":\"Error: failed to schedule userflow test\\nTypeError: The \\\"input\\\" argument must be ArrayBuffer. Received type object ([object Object])\"}"

函数非常简单:

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {    
    let response: APIGatewayProxyResult;
    let messageBody: string;

    if (!event.body) {
        response = {
            statusCode: 500,
            body: JSON.stringify({
                message: 'No Target Information Found',
            }),
        };
        return response;
    } else {
        response = {
            statusCode: 200,
            body: JSON.stringify({
                message: 'hello world',
            }),
        };
        messageBody = event.body;
    }

    const client = new SQSClient({ region: "us-east-1" });

    const params = {
        MessageBody: messageBody,
        QueueUrl: "https://sqs.us-east-1.amazonaws.com/41111111119/scheduled-userflows"
    }

    const command = new SendMessageCommand(params);

    try {
        await client.send(command);
    } catch (error) {
        response = {
            statusCode: 500,
            body: JSON.stringify({
                message: 'Error: failed to schedule userflow test\n' + error,
            }),
        };
    } finally {
        return response;
    }
};

那是什么意思?
为什么这种情况只发生在Lambda上,而不是在本地?
这是为了达到这个目的:How can I execute a puppeteer script in the cloud as a single task?

laik7k3q

laik7k3q1#

输入对象中的MessageBody属性应该是字符串,而不是对象。可以先将对象转换为字符串,然后再将其传递给MessageBody属性。

const params = {
        MessageBody: JSON.stringify(messageBody),
        QueueUrl: "https://sqs.us-east-1.amazonaws.com/41111111119/scheduled-userflows"
}

相关问题