我正在尝试batchWrite
使用DocumentClient
一组项目(JSON)中的DynamoDB 执行操作
这是我的代码:
var items = []; for (i = 0; i < orders.length; i++) { var ord = orders[i]; //a simple json object var item = { 'PutRequest': { 'Item' : ord[i] } }; items.push(item); } var params = { RequestItems: { 'my_table_name': items } }; var docClient = new AWS.DynamoDB.DocumentClient(); docClient.batchWrite(params, function(err, data) { if (err) { console.log('There was a problem putting the items in the table'); context.fail(err); } else { console.log('Items updated in table'); context.done(); } });
我收到以下错误:
{"errorMessage":"Missing required key 'Item' in params.RequestItems['my_table_name'] [0].PutRequest","errorType":"MissingRequiredParameter"...
我查看了文档,但我无法理解我做错了什么.
这是正确的方法:
function batchWrite(arrayOf25) { //25 is as many as you can write in one time var itemsArray = []; for (i = 0; i < arrayOf25.length; i++) { var someItem = arrayOf25[i]; var item = { PutRequest: { Item: someItem } }; if (item) { itemsArray.push(item); } } var params = { RequestItems: { 'my_table_name': itemsArray } }; //var AWS = require('aws-sdk'); //These should be added at the top //var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'}); docClient.batchWrite(params, function(err, data) { if (err) { console.log(err); } else { console.log('Added ' + itemsArray.length + ' items to DynamoDB'); } }); }