我正在学习使用 v8.10 完成的 lambda 课程。我正在尝试使用 nodejs v10.x,因为这是我将来要用于我的项目的内容。
我不明白为什么“const uuid = require('uuid');” 行仅在 nodejs v10 中引发导入错误,但在 v8.10 中代码运行得很好。
代码:
const aws = require('aws-sdk');
const s3 = new aws.S3();
const uuid = require('uuid');
exports.handler = async (event) => {
console.log("Get the event to our S3POC class - " + JSON.stringify(event));
const newUUID = uuid.v4();
console.log("The file name is:" + newUUID);
//put our sentence into the s3 bucket
return s3.putObject({
Bucket: "helloworld-s3.arkhadbot.com",
Key: "test" + ".json"
});
};
Run Code Online (Sandbox Code Playgroud)
错误
Response:
{
"errorType": "Runtime.ImportModuleError", …Run Code Online (Sandbox Code Playgroud) 我正在使用 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();
};
Run Code Online (Sandbox Code Playgroud)
我希望该函数仅调用一次,但它更经常在执行结果区域中打印相同的内容 2-3 次
Response:
{
"Item": {
"userId": "112",
"uuid": "0118bb6f-e361-42a6-85e5-043091b69389"
}
}
Request ID:
"4f5ce9da-bbf2-408b-9175-2759f45ba4fe"
Function …Run Code Online (Sandbox Code Playgroud)