Lambda 函数跳过 SNS 发布调用
Lambda function skipping SNS publish call
我目前正在尝试从 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 = 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
};
}
};
我已经测试过 for each 循环根据返回的项目数正确运行,并且扫描的数据确实都是正确的,但发布部分不是 运行
这是我的 firebase 消息服务代码
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
messageBody = message.GetNotification().Body;
}
// NOTE: test messages sent via the Azure portal or AWS SNS portal will be received here
else
{
messageBody = message.Data.Values.First();
}
// convert the incoming message to a local notification
SendLocalNotification(messageBody);
}
您可以return所有的 sns 发布 promise 并将它们收集在一个数组中,然后在所有 promise 数组上使用 await。
const publishPromises = allItems.map(function (item){
......
return sns.publish(msgparams).promise();
});
await Promise.all(publishPromises);
我目前正在尝试从 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 = 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
};
}
};
我已经测试过 for each 循环根据返回的项目数正确运行,并且扫描的数据确实都是正确的,但发布部分不是 运行
这是我的 firebase 消息服务代码
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
messageBody = message.GetNotification().Body;
}
// NOTE: test messages sent via the Azure portal or AWS SNS portal will be received here
else
{
messageBody = message.Data.Values.First();
}
// convert the incoming message to a local notification
SendLocalNotification(messageBody);
}
您可以return所有的 sns 发布 promise 并将它们收集在一个数组中,然后在所有 promise 数组上使用 await。
const publishPromises = allItems.map(function (item){
......
return sns.publish(msgparams).promise();
});
await Promise.all(publishPromises);