真的不知道如何解决以下错误。当用户通过我的网站提交联系我们表单页面提交时,我尝试返回更详细的响应。
{
"errorType": "SyntaxError",
"errorMessage": "Unexpected token o in JSON at position 1",
"trace": [
"SyntaxError: Unexpected token o in JSON at position 1",
" at JSON.parse (<anonymous>)",
" at Runtime.exports.handler (/var/task/index.js:6:53)",
" at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:74:25)"
]
}
Lambda 函数如下:
const aws = require("aws-sdk");
const ses = new aws.SES({ region: "us-east-1" });
exports.handler = async function (event) {
console.log('EVENT: ', event)
// Extract the properties from the event body
const { senderEmail, senderName, message } = JSON.parse(event.body)
const params = {
Destination: {
ToAddresses: ["[email protected]"],
},
// Interpolate the data in the strings to send
Message: {
Body: {
Text: {
Data: `You just got a message from ${senderName} - ${senderEmail}:
${message}`
},
},
Subject: { Data: `Message from ${senderName}` },
},
Source: "[email protected]",
};
return ses.sendEmail(params).promise();
};
我遇到了同样的问题,并通过从事件主体中删除
JSON.parse
来解决它。看起来 event
负载会自动从 JSON 解析为 JS 对象。
const { senderEmail, senderName, message } = event.body
或
const { senderEmail, senderName, message } = JSON.parse(JSON.stringify(event.body))
希望有帮助。
附注我正在使用
node 16.x
运行时