2016-05-17 109 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