Node.js Lambda函数跳过SNS发布调用
我目前正试图从AWS Lambda发送推送通知,但在我的函数中跳过了以下代码,因为我试图在其中输出一些内容,但它没有显示出来。 请帮帮我Node.js Lambda函数跳过SNS发布调用,node.js,aws-lambda,amazon-sns,Node.js,Aws Lambda,Amazon Sns,我目前正试图从AWS Lambda发送推送通知,但在我的函数中跳过了以下代码,因为我试图在其中输出一些内容,但它没有显示出来。 请帮帮我 const AWS = require("aws-sdk"); const documentClient = new AWS.DynamoDB.DocumentClient(); const sns = new AWS.SNS(); AWS.config.region = 'ap-southeast-1' exports.handler
const AWS = require("aws-sdk");
const documentClient = new AWS.DynamoDB.DocumentClient();
const sns = new AWS.SNS();
AWS.config.region = 'ap-southeast-1'
exports.handler = async event => {
const params = {
TableName: "Items" // The name of your DynamoDB table
};
try {
// Utilising the scan method to get all items in the table
const data = await documentClient.scan(params).promise();
var allItems = data.Items;
allItems.forEach(async function (item){
const tempItem = item;
var endDate = new Date(tempItem.endDate);
var today = new Date();
today.setHours(0, 0, 0, 0);
var currentDaysLeft = endDate - today;
if (currentDaysLeft < 0)
{
currentDaysLeft = 0;
}
// convert JS date milliseconds to number of days
currentDaysLeft = Math.ceil(currentDaysLeft / (1000 * 60 * 60 * 24));
if (tempItem.durationLeft != currentDaysLeft){
tempItem.durationLeft = currentDaysLeft;
}
const message = tempItem.username + "'s " + tempItem.title + " is expiring in " + tempItem.durationLeft + " days." ;
const msgparams = {
Message: message,
TopicArn: 'arn:aws:sns:ap-southeast-1:xxxxxxxxx:xxxxx'
};
var publishPromise = sns.publish(msgparams).promise();
});
return;
} catch (e) {
return {
body: e
};
}
};
您可以返回所有sns发布承诺,并将其收集到一个数组中,然后在所有承诺数组上使用wait
const publishPromises = allItems.map(function (item){
......
return sns.publish(msgparams).promise();
});
await Promise.all(publishPromises);
你能添加完整的lambda代码吗?编辑我的帖子,显示我的lambda代码在
sns.publish(msgparams.promise()之前等待丢失代码>?我添加了wait关键字,但它仍然不起作用这是正确的一步,因为推送通知是通过发送的,但我只收到1个推送通知,而应该总共有6个。现在我想知道这是否是代码或AWS SNS的限制你的意思是说allItems长度为6,PublishPromissions长度为1?您可以添加一些调试语句来检查上述两个数组的大小,并在map函数中添加一个log语句来检查它执行了多少次。我刚刚做了测试,publishPromises的大小为6,日志显示了6个log语句,但使用的手机im只收到一个通知。我使用的是API为27的android,如果这很重要的话,你可以向你的SNS添加一个sqs订阅,并验证你在那里收到了多少消息。您应该在那里收到6条消息。sqs订阅确实显示发送了6条消息,但收到的消息数为0
const publishPromises = allItems.map(function (item){
......
return sns.publish(msgparams).promise();
});
await Promise.all(publishPromises);