AWS DynamoDb DocumentClient - 从项目数组创建 batchWrite - node.js

AWS DynamoDb DocumentClient - Creating a batchWrite from an array of Items - node.js(AWS DynamoDb DocumentClient - 从项目数组创建 batchWrite - node.js)
本文介绍了AWS DynamoDb DocumentClient - 从项目数组创建 batchWrite - node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用来自项目数组 (JSON) 的 DynamoDB 的 DocumentClient 执行 batchWrite 操作

I'm trying to perform a batchWrite operation using DynamoDB's DocumentClient from an array of items (JSON)

这是我的代码:

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"...

我查看了 文档 但我不明白我在说什么我做错了.

I have looked at the documentation but I can't understand what I am doing wrong.

推荐答案

以下是正确的做法:

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');

      }   


    });


}

这篇关于AWS DynamoDb DocumentClient - 从项目数组创建 batchWrite - node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)