gpt4 book ai didi

node.js - 为什么 AWS Lambda 函数返回 null?

转载 作者:搜寻专家 更新时间:2023-11-01 00:47:32 24 4
gpt4 key购买 nike

我的 AWS Lambda 函数通过 SNS 发送电子邮件,但 Lambda 控制台中的响应为空。我使用了 AWS 提供的蓝图,并使用 Node.js 10.x 将代码放入 Lambda 处理程序中。

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/sns/sns_publishtotopic.js

我在 node.js 中使用 promises 方面经验不足,我对 Stack Overflow 的研究似乎表明这可能是这里的问题。

'use strict';

var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'us-west-2'});


exports.handler = (event, context, callback) => {
// Create publish parameters
var params = {
//Message: `The fluid level is low on your Knight system: ${event.my_volume} mL.`,
Message: `The fluid level is low on your Knight system.`,
Subject: `Low Fluid Level Alert - Knight`,
TopicArn: `arn:aws:sns:us-west-2:468820349153:MyLowVolumeTopic`,
};

// Create promise and SNS service object
var publishTextPromise = new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();

// Handle promise's fulfilled/rejected states


publishTextPromise.then(
function(data) {
console.log("Message: " + params.Message);
console.log("Sent to the topic: " + params.TopicArn);
console.log("MessageID is " + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
};

结果是我收到的电子邮件信息不一致,并且在 Lambda 控制台中看到空响应。这是一个示例日志结果:

Response: null
Request ID: "9dcc8395-5e17-413a-afad-a86b9e04fb97"

Function Logs:
START RequestId: 9dcc8395-5e17-413a-afad-a86b9e04fb97 Version: $LATEST
2019-08-17T21:44:31.136Z 9dcc8395-5e17-413a-afad-a86b9e04fb97 Message: The fluid level is low on your Knight system.
2019-08-17T21:44:31.136Z 9dcc8395-5e17-413a-afad-a86b9e04fb97 Sent to the topic: arn:aws:sns:us-west-2:468820349153:MyLowVolumeTopic
2019-08-17T21:44:31.136Z 9dcc8395-5e17-413a-afad-a86b9e04fb97 MessageID is cb139fb6-5d37-574e-9134-ca642a49fde5
END RequestId: 9dcc8395-5e17-413a-afad-a86b9e04fb97
REPORT RequestId: 9dcc8395-5e17-413a-afad-a86b9e04fb97 Duration: 1367.98 ms Billed Duration: 1400 ms Memory Size: 128 MB Max Memory Used: 75 MB

最佳答案

您的 lambda 在完成 promise 之前退出。要避免这种情况,您可以使用 async-await:

exports.handler = async (event, context, callback) => {
const data = await publishTextPromise;
//maybe some console log here
return data;
}

或者,您可以返回 promise ,返回 publishTextPromise.then(...).catch(...)

关于node.js - 为什么 AWS Lambda 函数返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com