当我单击"测试"按钮时,AWS lambda函数启动两次



我使用的是chrome浏览器,当我单击屏幕右上角的测试按钮时,lambda函数似乎会触发1-3次,我不明白为什么会发生这种情况。

我已经尝试过将参数直接输入到dynamoDB.get调用中,并在谷歌上搜索了一段时间,试图找到有类似问题的人。我发现一些非常接近,但没有一个谈到在使用内置测试按钮时一个函数多次触发。我还尝试过异步调用并等待它,但都无济于事。

// Import Libraries
const aws = require('aws-sdk');
const dynamoDB = new aws.DynamoDB.DocumentClient();
aws.config.update({
region: "us-east-1"
});
// Get Document
exports.handler = async (event, context) => {
let params = {
TableName: event.TableName,
Key: {
uuid: event.uuid
} 
};  
return await dynamoDB.get(params, function(error, data){
if(error){
console.error("Error", error);
}
else{
console.log("Data: ", data);
}
}).promise();
};

我希望函数只调用一次,但它更经常在执行结果区域中打印2-3次相同的东西

Response:
{
"Item": {
"userId": "112",
"uuid": "0118bb6f-e361-42a6-85e5-043091b69389"
}
}
Request ID:
"4f5ce9da-bbf2-408b-9175-2759f45ba4fe"
Function Logs:
START RequestId: 4f5ce9da-bbf2-408b-9175-2759f45ba4fe Version: $LATEST
2019-11-06T01:46:01.361Z    4f5ce9da-bbf2-408b-9175-2759f45ba4fe    
INFO    Data:  { Item:
{
"userId": "112",
"uuid": "0118bb6f-e361-42a6-85e5-043091b69389"
} } 
2019-11-06T01:46:01.441Z    4f5ce9da-bbf2-408b-9175-2759f45ba4fe    
INFO    Data:  { Item:
{
"userId": "112",
"uuid": "0118bb6f-e361-42a6-85e5-043091b69389"
} } 
2019-11-06T01:46:01.461Z    4f5ce9da-bbf2-408b-9175-2759f45ba4fe    
INFO    Data:  { Item:
{
"userId": "112",
"uuid": "0118bb6f-e361-42a6-85e5-043091b69389"
} } 
END RequestId: 4f5ce9da-bbf2-408b-9175-2759f45ba4fe
REPORT RequestId: 4f5ce9da-bbf2-408b-9175-2759f45ba4fe  Duration: 127.68 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 95 MB  

您正在提供回调方法您正在请求同一API调用中的promise。

你不应该两者都做。我建议删除回调,例如:

exports.handler = async (event, context) => {
const params = {
TableName: event.TableName,
Key: {
uuid: event.uuid
} 
};
try {
const data = await dynamoDB.get(params).promise();
console.log("Data: ", data);
} catch(error) {
console.error("Error:", error);
}
};

最新更新