2

I'm having trouble with the AWS DynamoDb JS SDK v2.4.9. I want to use the DocumentClient class as opposed to the lower level DynamoDb class, but can't get it working.

This works:

function testPutItem( callback ) {

    var tableName = 'todos';

    var params = {
        TableName: tableName,
        Item: {
            user_id: { S : userId },
            id: { N : msFromEpoch },        // ms from epoch
            title: { S : makeRandomStringWithLength(16) },
            completed: { BOOL: false }
        }
    };

    var dynamodb = new AWS.DynamoDB();
    dynamodb.putItem(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else {
            console.log(data);           // successful response
            if (callback) callback(data);
        }
    });
}

This does not work and gives the error InvalidParameterType: Expected params.Item[attribute] to be a structure for each attribute--as if DocumentClient is expecting the same input as DynamoDb:

function testPutItem( callback ) {

    var tableName = 'todos';

    var params = {
        TableName: tableName,
        Item: {
            user_id: userId,
            id: msFromEpoch,
            title: makeRandomStringWithLength(16),
            completed: false
        }
    };
    console.log(params);

    var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
    docClient.put(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else {
            console.log(data);           // successful response
            if (callback) callback(data);
        }
    });
}

Does anyone have any idea what I am doing wrong?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
pitachip
  • 965
  • 3
  • 7
  • 24
  • I just tried your example and it worked for me; I used dynamodb-local for testing, and created a table named `todos`; and was successfully able to post an item. – jacob Aug 01 '16 at 22:15
  • Thank you! One step closer, but also more confused since I pretty much have no other relevant code to debug. Is there any other js file besides the main aws-sdk-.min.js file I should be including to get all the functionality of the DocumentClient? – pitachip Aug 02 '16 at 14:40
  • If it helps; I was using 2.4.11; I didn't think to check the version. Could you update to that version? Seems to be the latest on npm. – jacob Aug 02 '16 at 17:56

1 Answers1

0

I used to have the same issue,

please try with a simple object first, cause it's due to some special characters in your attributes, see my example :

this generates the error

InvalidParameterType: Expected params.Item[attribute] to be a structure

 var Item = {
  domain: "knewtone.com",
  categorie: "<some HTML Object stuff>",
  title: "<some HTML stuff>",
  html: "<some HTML stuff>""
};

but when i replace the HTML stuff with a formated Html, simple characters , it works

var Item = {
  domain: "knewtone.com",
  categorie: $(categorie).html(),
  title: $(title).html(),
  html: $(html).html()
};
Sid Ali
  • 1,779
  • 4
  • 17
  • 38