Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Expected params.Item['id'].N to be a string

Here's the deal:

I am not able to add data to DynamoDB. JS file is in S3, here's the code:

var date = new Date(),
    item = {
        'id': {N: date.getTime()},
        'user': {S: "Sam"}
    },
    params = {
        TableName: 'users',
        Item: item
    };
dynamodb.putItem(params,function(err,data){
    if (err) {
        console.log(err);
    } else {
        console.log("user added to DynamoDB");
    }
});

Here's the screenshot from the DB which means that the id should be a number, not string:

enter image description here

The error I get:

Error: Expected params.Item['id'].N to be a string

at ParamValidator.fail (aws-sdk.js:96666)
at ParamValidator.validateType (aws-sdk.js:96827)
at ParamValidator.validateString (aws-sdk.js:96762)
at ParamValidator.validateScalar (aws-sdk.js:96742)
at ParamValidator.validateMember (aws-sdk.js:96709)
at ParamValidator.validateStructure (aws-sdk.js:96690)
at ParamValidator.validateMember (aws-sdk.js:96703)
at ParamValidator.validateMap (aws-sdk.js:96729)
at ParamValidator.validateMember (aws-sdk.js:96707)
at ParamValidator.validateStructure (aws-sdk.js:96690)
like image 684
AmazingDayToday Avatar asked May 16 '17 20:05

AmazingDayToday


2 Answers

I just had the issue myself.

Dynamo expect the "N" value to be a string, it's a bit misleading since you expect to pass a number to it.

where you have your date.getTime(), try to translate it into a string.

'id': { N: date.getTime().toString() },

That worked for me.

like image 184
Rafael Avatar answered Sep 23 '22 18:09

Rafael


Even when you want to save the value as a number in DynamoDB, you still need to send a string to the SDK, and so, you'll need to:

var date = new Date(),
    item = {
        'id': {N: date.getTime().toString()},
        'user': {S: "Sam"}
    },
    params = {
        TableName: 'users',
        Item: item
    };
dynamodb.putItem(params,function(err,data){
    if (err) {
        console.log(err);
    } else {
        console.log("user added to DynamoDB");
    }
});
like image 25
Oded Breiner Avatar answered Sep 21 '22 18:09

Oded Breiner