I am trying to use node.js to insert an record to dynamoDB in lambda, but get this error,
Part of the source :
exports.handler = (event, content, callback) => {
var params = {
Item: {
"UserId": {
S: "u_"+Math.random()
},
"Age": {
N: event.age
}
},
TableName: "userdata"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
callback(null, data);
}else{
console.log(data); // successful response
callback(null, data);
}
});
};
Json data in my request body:
{
"age":12
}
Solution 1:
DynamoDB expect the N value is a string, but not in number format, so you need to convert the value in number format before you call putItem method. The first
exports.handler = (event, content, callback) => {
var params = {
Item: {
"UserId": {
S: "u_"+Math.random()
},
"Age": {
N: event.age.toString()
}
},
TableName: "userdata"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
callback(null, data);
}else{
console.log(data); // successful response
callback(null, data);
}
});
};
Solution 2:
If you have use apiGateway, you can change the data at body mapping template in "Integration Request", by adding double quotation marks, an example:#set($inputRoot = $input.path('$'))
{
"age" : "$inputRoot.age"
}
No comments :
Post a Comment