2016-05-17 107 views
2

我试图使用Lambda(节点)更新DynamoDB中的记录。我可以更改params的结构并获得类似Expected params.ExpressionAttributeValues[':done'] to be a structure的错误,所以我相信它正在与DynamoDB进行通信。使用AWS Lambda的AWS DynamoDB中的UpdateItem(节点)

这是PARAMS:

{ 
    "TableName": "test_table", 
    "Key": { 
     "id": { 
      "S": "90c31f23-96e3-4d5d-b08d-95aafb9bed2e" 
     } 
    }, 
    "UpdateExpression": "SET done = :done", 
    "ExpressionAttributeValues": { 
     ":done": { 
      "S": "t" 
     } 
    }, 
    "ReturnValues": "UPDATED_NEW" 
} 

从那里它只是超时,所以很难知道问题是什么。

这是完整的lambda函数:

var aws = require('aws-sdk'); 
var dynamodb = new aws.DynamoDB({ 
    apiVersion: '2012-08-10', 
    accessKeyId: 'xxx', 
    secretAccesskey: 'xxx', 
    region: 'us-west-2' 
}) 

exports.handler = (event, context, callback) => { 
    console.log('Incoming: ', event); 

    var table = "test_table"; 

    event.Records.forEach((record) => { 
     console.log('DynamoDB Record: %j', record.dynamodb); 

     var params = { 
     TableName: table, 
     Key: { 
      "id": record.dynamodb.Keys.id 
     }, 
     UpdateExpression: "SET done = :done", 
     ExpressionAttributeValues: { 
      ":done": { "S": "t" } 
     }, 
     ReturnValues: "UPDATED_NEW" 
     }; 

     console.log("params: %j", params); 

     dynamodb.updateItem(params, function(err, data) { 
     if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2)); 
     else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2)); 
     }); 
    }); 
    callback(null, "Successfully processed ${event.Records.length} records."); 
}; 
+0

是正确的,该消息不是为了创建表格,而是将文档更新为预先存在的表格?如果是这样,你会请包括创建表info – aug2uag

+0

我正在使用与创建表的动态轨道。有没有特别想知道的东西? – Archonic

+0

Schemas是严格的,根据外观来判断它需要完全匹配..检查[低级别类型系统](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.LowLevel .Walkthrough.html) – aug2uag

回答

3

根据文档,你得有一个文档的客户端,并细化流量控制..

var aws = require('aws-sdk'); 
var dynamodb = new aws.DynamoDB({ 
    apiVersion: '2012-08-10', 
    accessKeyId: 'xxx', 
    secretAccesskey: 'xxx', 
    region: 'us-west-2' 
}) 

exports.handler = (event, context, callback) => { 
    console.log('Incoming: ', event); 

    var table = "test_table"; 
    var docClient = new dynamodb.DocumentClient() 

    function async(record, next) { 
     var params = { 
      TableName: table, 
      Key: { 
       "id": record.dynamodb.Keys.id 
      }, 
      UpdateExpression: "SET done = :done", 
      ExpressionAttributeValues: { 
       ":done": { "S": "t" } 
      }, 
      ReturnValues: "UPDATED_NEW" 
     }; 

     console.log("params: %j", params); 

     docClient.updateItem(params, function(err, data) { 
      if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2)); 
      else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2)); 
      next() // modify for err handling 
     }); 
    } 
    function final() { callback(null, "Successfully processed ${event.Records.length} records."); } 

    var results = []; 

    event.Records.forEach(function(item) { 
     async(item, function(){ 
      results.push(true); 
      if(results.length == event.Records.length) final(); 
     }); 
    }); 
}; 
+0

目前我得到'TypeError:dynamodb.DocumentClient不是函数'。不应该很难解决这个问题。 – Archonic

+0

现在它是'TypeError:docClient.updateItem不是函数'-_-。 'docClient.update'不会抛出错误但超时。 – Archonic

+0

你能看到它向亚马逊请求吗? – aug2uag