AWS DynamoDb DocumentClient - Creating a batchWrite from an array of Items - node.js

前端 未结 1 1635
情歌与酒
情歌与酒 2021-02-09 11:52

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

This is my code:

v         


        
相关标签:
1条回答
  • 2021-02-09 12:19

    Here is the correct way to do it:

    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');
    
          }   
    
    
        });
    
    
    }
    
    0 讨论(0)
提交回复
热议问题