Creating the table works fine and when I insert an item it says the item inserted successfully, but shows an empty item was inserted.
"Added item:{}"
It should instead show the item has a username of 'bob' and a password of 'password'. I am using the most recent versions of nodejs, npm, dynamodb local, and the javascript aws-sdk. When I try to get the item it says it is unidentified.
var dynamodb = new AWS.DynamoDB();
var dynamodbDoc = new AWS.DynamoDB.DocumentClient();
var createUsersTable= {
TableName : "Users",
KeySchema: [
{ AttributeName: "username", KeyType: "HASH"}
],
AttributeDefinitions: [
{ AttributeName: "username", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(createUsersTable, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
setTimeout( function(){
var table = "Users";
var username = 'bob';
var params = {
TableName:table,
Item:{
"username": 'bob',
"password": 'password'
}
};
console.log("Adding a new item...");
dynamodbDoc.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));
}
});
}, 4000 );
@susanlinsfu
Have you tried setting the ReturnValues parameter on your put operation to something like ALL_NEW? The operation allows you to control what data you get back, such as just updated fields, all fields, etc.
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
Let me know if this resolves your issue!
@chrisradek
I have changed my params to now look like this:
var params = {
TableName: table,
Item:{
"username": 'bob'
},
ReturnValues: 'ALL_NEW'
};
But get the error message when I try to insert now:
Unable to add item. Error JSON: {
"message": "Return values set to invalid value",
"code": "ValidationException",
"time": "2015-11-18T03:25:33.853Z",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
@susanlinsfu
It looks like I need to update the documentation for ReturnValues for the DynamoDBClient. The put operation calls the dynamodb putItem operation. If you take a look at those docs here:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property
The return value from putItem, and also put is the item attributes as they appeared before they were updated by either operation. ALL_OLD will return the content of the old item, and NONE (default) will prevent anything from being returned.
@chrisradek Thanks Chris, I have just tried ALL_OLD, and NONE and these work!
@chrisradek Since put and putItem doesn't return the new item attributes through ReturnValues, is there a way to have it return the newly inserted entry without querying the table separately with a get?
+1 for cleung2010 question
I just had the same issue as @susanlinsfu. Changed ReturnValues to ALL_OLD and it worked.
However, I wonder if there is way to return the actual NEW values.
A note was added in https://github.com/aws/aws-sdk-js/commit/9ddc7fb192f7303ff80cc26db84c8ca4e324f416 spelling out which ReturnValues value is accepted by both PutItem and DeleteItem.
@alayor When making a PutItem request, you're overwriting the entire item, so if the request succeeds then the new values are the ones provided as input parameters. If you're updating an item in place with an UpdateItem operation, you can use ALL_NEW to get the updated value.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.
Most helpful comment
@chrisradek Since
putandputItemdoesn't return the new item attributes throughReturnValues, is there a way to have it return the newly inserted entry without querying the table separately with aget?