13

New to AWS, trying to put data into a table. After reading the documentation and trying to follow examples I am running into this validation error.

One of the required keys was not given a value

My code:

var conf = require("/directory");
var AccessKey = 'supersecretkey';
var SecretKey = 'supersecretkey';

var tableName = conf.tableCS;

var AWS = require('aws-sdk');

console.log("Endpoint: " + conf.endpoint);
AWS.config.update({
    accessKeyId: AccessKey,
    secretAccessKey: SecretKey,
    region: conf.region,
    endpoint: conf.endpoint
});
var docClient = new AWS.DynamoDB.DocumentClient();

var file = process.argv[2];
console.log("File: " + file);

var csv = require("fast-csv");

csv.fromPath(file)
    .on("data", function(data) {
        // Uncomment to see CSV data
        // console.log(data);

        var arrayLength = data.length;
        for (var i = 0; i < arrayLength; i++) {
            // console.log(data[i]);
            var params = {
                TableName: tableName,
                Key: {
                       RefID: data
                }
            };

            console.log("Adding a new item...");
            docClient.put(params, function(err, data) {
                if (err) {
                    console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
                }
                else {
                    console.log("Added item:", JSON.stringify(data, null, 2));
                }
            });
            // Looping through, now putItem into database
        }

    })
    .on("end", function() {
        console.log("done");
    });

I found this, but am not understanding the range key part. I'm creating the table in this following code and have the hash key, but not a range key. Is that my issue? What would I make the range key?

var conf = require("/directory");
var AccessKey = 'supersecretkey';
var SecretKey = 'supersecretkey';

var tableName = conf.tableCSV;

// Take input from command line
process.argv.forEach(function(val, index, array) {
    console.log(index + ': ' + val);
});

console.log("Table: " + tableName);

var AWS = require('aws-sdk');

console.log("Endpoint: " + conf.endpoint);
AWS.config.update({
    accessKeyId: AccessKey,
    secretAccessKey: SecretKey,
    region: conf.region,
    endpoint: conf.endpoint
});
var dynamodb = new AWS.DynamoDB();

var params = {
    TableName: tableName,
    KeySchema: [{
        AttributeName: 'RefID',
        KeyType: 'HASH'
    }],
    AttributeDefinitions: [{
        AttributeName: 'RefID',
        AttributeType: 'S'
    }],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    }
};

dynamodb.createTable(params, function(err, table) {
    if (err) {
        console.log(err);
    }
    else {
        console.log("TABLED CREATED");
        console.log(table);
    }
});
Community
  • 1
  • 1
FF5Ninja
  • 515
  • 2
  • 7
  • 17
  • The OP edited the code above to fix the issue highlighted by the accepted answer, so now the accepted answer doesn't make sense. I don't know why this seemed like a good idea. – Tony Aug 24 '23 at 18:25

1 Answers1

19

In the table definition, you named the key Ref-ID but in your put operation you are naming the key Info. If your table has a hash key named Ref-ID then every record you insert will require a value for Ref-ID.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • Thanks for the response. I made the changes and am still getting the same error. I edited my post to reflect the changes I made, perhaps I made another trivial mistake? – FF5Ninja Aug 02 '16 at 15:42
  • 2
    Did you recreate the table after changing the key name? – Mark B Aug 02 '16 at 15:48