最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Attribute may not contain an empty string - Stack Overflow

programmeradmin3浏览0评论

I am 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.

I am 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.

Share Improve this question edited Sep 10, 2018 at 17:39 coffee-grinder 27.6k19 gold badges59 silver badges82 bronze badges asked Aug 3, 2016 at 18:29 FF5NinjaFF5Ninja 5252 gold badges7 silver badges17 bronze badges 8
  • That error would mean that some of your data is not in your table. If that error occurs; that should mean that Item didn't get inserted. And what do you mean about the data being in the wrong order? There's no real order to your data unless you supply a range key? – jacob Commented Aug 3, 2016 at 18:55
  • Ah, you are correct about the order. Ignore I said that (I edited it out). My primary concern right now is this error that I can't seem to fix. I test the table after I get all the errors, and the data is in there. But it would seem to be after attempting many times over. – FF5Ninja Commented Aug 3, 2016 at 18:58
  • Could it be possible that one of the fields your accessing on data doesn't actually exist? I see you're accounting for empty strings in your iteration; but it doesn't account for a potential typo in an expected key name; or if the key-name merely doesn't exist? – jacob Commented Aug 3, 2016 at 19:04
  • Well I just looked again to account for that, and I don't see any problems with typos or missing data. I could be wrong though. Something interesting though, even when it successfully adds the data, if I try console.log the data, it appears as "undefined"... Added an edit to explain what I mean. – FF5Ninja Commented Aug 3, 2016 at 19:11
  • 1 Why don't you consider moving your for (var key in data) code block to occur after you initialize params; and have it run over the parameters you're setting to verify that those are indeed not null; I'd then make sure to print where you're setting those fields. – jacob Commented Aug 3, 2016 at 19:32
 |  Show 3 more ments

2 Answers 2

Reset to default 7

It appears that dynamoDB at this time does not allow empty strings. I can NOT understand why, but as of this date you cannot not store an attribute of "Key":"".

Please plain to amazon about it. key="" and key=null are very different use cases and are needed.

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!")
    }
  }
}
发布评论

评论列表(0)

  1. 暂无评论