Search code examples
javascriptcsvamazon-web-servicesamazon-dynamodb

Empty String Validation Exception - DynamoDB


I have scoured the internet on how to fix this with little success...

I am parsing a CSV and putting the data into a table using DynamoDB. Anytime there is a blank entry, I receive the error:

One or more parameter values were invalid: An AttributeValue may not contain an empty string

For example:

Header: "Name","Age","Birth date"
Entry:  Brandon,22  <------ THROWS ERROR

The problem is, I will never know if there are blanks or not in the CSV. But I still need to parse it even if there is.

I have attempted to re-assign the value of the empty string to something like "N/A" in an effort to subvert this error to no avail. Any suggestions?

EDIT: Code to add context.

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

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

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

        params = {
            TableName: tableName,
            Item: {
                RefID: {
                    S: data["Ref-ID"]
                },
                //lots more items
            }
        };

        //Validation loops to make sure the items are defined
        for (var key in params.Item) {
            for (var items in params.Item[key]) {
                var value = params.Item[key][items];
                if (value === undefined || value === "") {
                    value = "N/A";
            }
        }

        dynamodb.putItem(params, function(err, info) {
            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));
            }
        });
        // }

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

Solution

  • Nowhere in your code are you assigning the values of name, age, etc. into your params object. Are you still not showing all the code? Disregarding that for a minute and looking at your validation loop, you are just throwing away the "N/A" value. You need to modify the actual params object you are using to insert records. Change the validation loop to this:

        //Validation loops to make sure the items are defined
        for (var key in params.Item) {
            for (var items in params.Item[key]) {
                var value = params.Item[key][items];
                if (value === undefined || value === "") {
                    params.Item[key][items] = "N/A";
            }
        }
    

    There's probably an easier way to do this as you are taking the values from data and copying that to the params object, but you don't seem to be showing that code.