gpt4 book ai didi

node.js - 我的 Lambda 在代码完成之前结束 - node.js

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:46 24 4
gpt4 key购买 nike

我知道以前有人以各种方式问过这个问题,但我无法弄清楚。我对 Node.js 和 lambda 还很陌生。如果我运行 lambda 两次,此代码将起作用,但第一次永远不会运行完成。如果我通过添加从本地 IDE 运行它,这也可以正常工作exports.handler();到代码块的末尾。

该代码查询 DynamoDB 的结果,然后尝试从 Dynamo 中删除这些记录。查询部分似乎每次都有效,但删除部分在第一次调用时未能发生。我似乎无法弄清楚 lambda 需要进行哪些更改才能等到我的所有流程完成。

提前致谢。

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({ region: 'us-east-2' });
exports.handler = async (event) => {
// Create DynamoDB service object
const ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-2" });
const tablename = process.env.table_name;

let dynapromises = [];

let params = {
ExpressionAttributeValues: {
':offNum': { S: process.env.cost_center },
':s': { N: '2' }
},
ExpressionAttributeNames: {
"#notif_status": "status"
},
KeyConditionExpression: 'officeNumber = :offNum',
TableName: tablename,
IndexName: 'officeNumberIndex',
ProjectionExpression: "notificationNumber",
FilterExpression: '(attribute_not_exists(#notif_status) or #notif_status = :s) and attribute_not_exists(statusTimes)'
};

let qresults = await ddb.query(params).promise();
console.log("Count of notifs again " + qresults.Items.length);

qresults.Items.forEach(function(element, index, array) {
console.log(element.notificationNumber.S);
let delparams = {
TableName: tablename,
ReturnValues: "ALL_OLD",
Key: {
notificationNumber: {
S: element.notificationNumber.S
}
}
};

dynapromises.push(ddb.deleteItem(delparams).promise().then(function(data) {
console.log("Deleted Record:"+ JSON.stringify(data)); // successful response
}, function(error) {
console.log(error, error.stack); // an error occurred
}));
console.log("deletion parameters " + JSON.stringify(delparams));
});
Promise.all(dynapromises).then(res => {
console.log("All promises done");
});
return qresults.Items.length;
};

最佳答案

问题在于您在所有 promise 完成之前返回,您需要将 return qresults.Items.length; 移到最后一个 then 内。

尝试使用此代码:

** 更新:使用工作代码更改代码片段 **

// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// Set the region
AWS.config.update({ region: 'us-east-2' });
exports.handler = async (event) => {
// Create DynamoDB service object
const ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-2" });
const tablename = process.env.table_name;

let params = {
ExpressionAttributeValues: {
':offNum': { S: process.env.cost_center },
':s': { N: '2' }
},
ExpressionAttributeNames: {
"#notif_status": "status"
},
KeyConditionExpression: 'officeNumber = :offNum',
TableName: tablename,
IndexName: 'officeNumberIndex',
ProjectionExpression: "notificationNumber",
FilterExpression: '(attribute_not_exists(#notif_status) or #notif_status = :s) and attribute_not_exists(statusTimes)'
};

let qresults = await ddb.query(params).promise();
console.log("Count of notifs again " + qresults.Items.length);

const dynapromises = qresults.Items.map( async element => {
let delparams = {
TableName: tablename,
ReturnValues: "ALL_OLD",
Key: {
notificationNumber: {
S: element.notificationNumber.S
}
}
};

try {
console.log("deletion parameters " + JSON.stringify(delparams));
const data = await ddb.deleteItem(delparams).promise();
console.log( "Deleted Record:"+ JSON.stringify(data) );
} catch ( err ) {
console.log(error, error.stack); // an error occurred
}
} )

await Promise.all(dynapromises)

console.log("All promises done");
return qresults.Items.length;
};

关于node.js - 我的 Lambda 在代码完成之前结束 - node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844202/

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