单击测试按钮时,AWS Lambda函数将触发两次 [英] AWS lambda function firing twice when I click the Test button
问题描述
我正在使用chrome浏览器,当我单击屏幕右上角的测试"按钮时,lambda函数似乎启动了1-3次,我无法弄清楚为什么会发生这种情况.
I am using chrome browser and when I click the Test button in the top right side of my screen the lambda function seems to fire 1-3 times and I cannot figure out why this is happening.
我尝试将参数直接放入dynamoDB.get调用中,并进行了一段时间的搜索,并试图找到存在类似问题的人员.我发现有一些接近,但是没有一个谈论使用内置测试按钮时多次触发单个函数.我也尝试过使呼叫异步并等待它,但是全部无济于事.
I have tried putting the parameters directly into the dynamoDB.get call as well as googling for a while and trying to find someone with a similar issue. I found some that were close, but none that talk about a single function firing multiple times while using the built in test button. I have also tried making the call asynchronous and await it but all to no avail.
// 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次
I would expect the function to call only once but it more often prints the same thing 2-3 times in the Execution Results area
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.
You are supplying a callback method and you are requesting a promise in the same API call.
您不应该两者都做.我建议删除回调,例如:
You should not do both. I recommend removing the callback, for example:
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);
}
};
这篇关于单击测试按钮时,AWS Lambda函数将触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!