Finn Cardiff Finn Cardiff - 1 month ago 25
Node.js Question

Saving an object to DynamoDB instance via NodeJS

I feel like I'm missing something obvious, but the documentation is a bit confusing for DynamoDB, especially for NodeJS (I do see a lot for Java, however!).

I've done quite a bit of searching and only have found questions pertaining to the old SDK, so hopefully this isn't a duplicate question!

I'm trying to store a Javascript object into my DynamoDB instance. The error I'm getting and the code I'm using is outlined below.

Error:

Unable to add item. Error JSON: {
"message": "One or more parameter values were invalid:
Type mismatch for key File expected: S actual: M",
"code": "ValidationException",
"time": "2016-10-29T19:36:02.317Z",
"requestId": [removed],
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}


Code:

var aws = require('aws-sdk');
var docClient = new aws.DynamoDB.DocumentClient({
"accessKeyId": AWS_ACCESS_KEY_ID,
"secretAccessKey": AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
endpoint: "https://dynamodb.us-east-1.amazonaws.com"
});
var tableName = AWS_TABLE_NAME;
var params = {
TableName: tableName,
Item: {
ProjID: projID,
File: {
name: fileName,
url: fileUrl
}
}
};
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));
}
callback(true);
});


Once again, I'm sure it's a fairly simple issue - I just haven't quite found any documentation to help me out that's updated.

Thanks in advance for any help you can give!

Answer

Is File an attribute in the schema of your table? If it is and it's defined as a String, then the error you are getting is just indicating that you are trying to put a map into a string attribute.

DynamoDB only supports String and Numeric keys so you'll have to revisit your design. Perhaps you can store the File information as a string for the key and then expanded as a map into a separate attribute if need be.