FF5Ninja FF5Ninja - 3 months ago 30
Javascript Question

Attribute may not contain an empty string

parsing a CSV file and putting the data in a table with AWS DynamoDB.

As it stands right now, I am getting the following error:

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


... BEFORE it puts the data in the table. The data is getting to the table, but not before spamming me with that error a million times.

My Code:

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

csv.fromPath(file, {
headers: true,
ignoreEmpty: true
})
.on("data", function(data) {

for (var key in data) {
if (data.hasOwnProperty(key)) {
if (data[key] === "" || data[key] === undefined || data[key] === null) {
data[key] = "N/A";
}
}

params = {
TableName: tableName,
Item: {
RefID: {
S: data["Ref-ID"]
},
//lots of other data
}
};
dynamodb.putItem(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));
}
});
}

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


As you can see, I am converting any possible empty strings to ==
N/A
in an attempt to solve this problem. Any thoughts?

EDIT:

This turns out to be
undefined
when it should display what it put in the table.

console.log("Added item:", JSON.stringify(data[key], null, 2));


EDIT 2: Changed this code...

dynamodb.putItem(params, function(err, data)


...to this:

dynamodb.putItem(params, function(err, info)


I am still getting the errors, but am now displaying the table correctly.

Answer

Try doing field validation on your param.Item Object to verify that everything is set properly; and find the errornous fields that are plaguing your console.

var tableName = "wombat_habitats";

var data = {
    "Ref-ID": "Charlie"
};

params = {
  TableName: tableName,
  Item: {
    RefID: {
      S: data["Ref-ID"]
    },
    SomethingElse: {
      S: data["Bad-Key"]
    }
    //lots of other data
  }
};

for(var itemKey in params.Item) {
    for(var itemAttr in params.Item[itemKey]) {
    var value = params.Item[itemKey][itemAttr];
    if(value === undefined || value === "") {
        console.log("item", itemKey, "of type", itemAttr, "is undefined!")
    }
  }
}
Comments