Trying to create a DynamoDB set.
var docClient = new AWS.DynamoDB.DocumentClient();
var set = docClient.createSet(["Red", "Green", "Blue"]);
console.log(set);
Correct log value of 'set':
Set {values: Array(3), type: "String"}
type: "String"
values: ["Red", "Green", "Blue"]
__proto__: Object
What I am getting in the console output:
constructor {values: Array(3), type: "String"}
type: "String"
values: ["Red", "Green", "Blue"]
__proto__: Object
Update call:
var params = {
TableName: "Table",
Key: {
"id": id
},
UpdateExpression: "ADD colors :c",
ExpressionAttributeValues: {
":c": docClient.createSet(["Red", "Green", "Blue"])
},
ReturnValues: "UPDATED_NEW"
}
docClient.update(params, callback);
Because the type of 'set' is a 'constructor' and not a 'Set'. I get the following error when trying to update the set in the DynamoDB.
Error: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operator type: MAP
I am really looking for any hint on what would cause this.
I am using webpack to deploy my app. I am able to get the correct value when running webpack in development, but when I build for production it exhibits the incorrect behavior.
This may not necessarily be an AWS issue. It may be a lack of javascript or webpack understanding on my part.
Ultimately it did turn out to be a webpack configuration issue on my part. The template I was using includes UglifyJS (v1.1.0) to optimize the code for production. It turns out that UglifyJS's default settings are to 'mangle' and 'compress'. Both of these options remove function names for code size optimization.
In my case this was a problem because the constructor for DynamoDBSet which is what 'createSet' calls has the name 'Set'. When docClient.update() is called it is expecting this function name in order to know how to assemble the DB update arguments.
The fix to not remove this function name in webpack.config.js:
plugins: [
new UglifyJSPlugin({
uglifyOptions: {
mangle: {
keep_fnames: true //Does not optimize function names
},
compress: {
keep_fnames: true //Same as mangle. Both are necessary
}
})
]