我正在尝试使用来自项目数组(JSON)的DynamoDB batchWrite
执行DocumentClient
操作
这是我的代码:
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"...
我看过documentation,但我无法理解我的意思 我做错了。
答案 0 :(得分:10)
这是正确的方法:
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');
}
});
}