2

我正在尝试将硬编码的数据项放入 DynamoDB。我正在使用 AWS 开发工具包对象来执行此更新。下面代码中的所有调试“Console.log”都被打印出来,但最终它会在 3.00 秒后打印 Task timed out

没有更新 DynamoDB

function updatedb(intent, session, callback) {
    let country;
     const repromptText = null;
     const sessionAttributes = {};
     let shouldEndSession = false;

console.log("In the function");
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });

var params = {
    TableName: "Location",
    Item: {
        "LocationID": { "S": "11" },
        "Country": { "S": "10" },
        "Description": { "S": "10" },
        "Name": { "S": "10" }
    }
};
console.log("Param loaded & executing the DocClient Put");

docClient.put(params, function (err, data) {
    if (err) {
        speechOutput = 'Update failed';
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
        callback(sessionAttributes,
            buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
        speechOutput = 'Update successful';
        callback(sessionAttributes,
            buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
    }
});
}

以下项目已被检查

1) DynamoDB 中有一个名为“Location”的表 2) DynamoDB 和这个 lambda 函数都在 ue-west-1 (Ireland) 3) 分配给这个 Lambda 函数的角色可以对这个表执行所有操作。请参阅下面的政策详情

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1510603004000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:eu-west-1:752546663632:table/Location"
        ]
    }
]
}

我的 Lambda 函数如何仅通过区域定位表“位置”?- 代码似乎没有端点等?- 只是根据教程开发的。

那是我想念的吗?

请问你能帮忙吗?

4

2 回答 2

2

我有一个类似的问题,请尝试将 require 语句放在函数的开头。

const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });
于 2018-02-25T20:45:32.217 回答
0

我相信 AWS 会根据您的身份,结合区域和表名来定位表。

我能够使用以下代码成功发布到表格:

const AWS = require('aws-sdk');

const dynamoDB = new AWS.DynamoDB({region: 'us-west-2'});

var params = {
  TableName: "my-table",
  Item: {
    "LocationID": { S: "11" },
    "Country": { S: "10" },
    "Description": { S: "10" },
    "Name": { S: "10" }
  }
};

dynamoDB.putItem(params, (err, data) => {
  if (err){
    console.error(err.stack);
  } else {
    console.log(data);
  }
});

如果您实际上可以从 CLI 发布到表格,那么至少还有一个问题:您似乎DocumentClient错误地使用了该类。看起来您将 for 的语法与 forDynamoDB.putItem的语法混淆了DynamoDB.DocumentClient.put

如果你注意到了,我的代码DynamoDB直接使用了这个类——根据你在做什么,我看不出你为什么不能这样做。否则,您应该更改Item对象:

var params = {
  TableName: "my-table",
  Item: {
    "LocationID": "11",
    "Country": "10",
    "Description": "10",
    "Name": "10"
  }
};

我的猜测是您的代码当前出错,因为您试图在要插入字符串的位置插入地图。如果您配置了 Cloudwatch,则可以检查日志。

最后,我没有看到您callback在代码中使用。如果您的意图是响应调用 lambda 的客户,您应该这样做。根据您的 NodeJS 版本,lambda 可以简单地超时而不会返回有用的响应。

于 2017-11-13T22:19:03.010 回答