Following the guide posted here: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
I am trying to put an object into my table.
UserService.ts
addUser(firstName: String, lastName: String) {
let params = {
TableName: 'MY_USERS_TABLE',
Item: {
id: uuid(),
firstName: firstName,
lastName: lastName
}
}
return Observable.fromPromise(database.put(params).promise())
}
Then in my component.ts
this.userService.addUser(this.firstName, this.lastName).subscribe((person) => {
console.log(JSON.stringify(person)
console.log(JSON.stringify(person.$response.data))
console.log(JSON.stringify(person.Attributes)
})
The output is:
JS: {}
JS: {}
JS: Undefined
What I'm trying to do is get back the data from the response of the call to the "PUT" function (EX: The data inserted from "ITEM" in the service)
MORE INFORMATION:
My IDE is telling me this is the (person) object that is passed into the subscribe function:
(parameter) person: DocumentClient.PutItemOutput & {
$response: Response<DocumentClient.PutItemOutput, AWSError>;}
Hi @westlakem,
By default, DynamoDB::PutItem (and, by extension, the DocumentClient's put method) returns no data. You must specify a ReturnValues parameter on the input to direct DynamoDB to return any attributes, and consumed capacity will only be returned if a ReturnConsumedCapacity parameter is included in the input. For example, if you wanted put to return the updated user record, UserService.ts would need to be updated to:
addUser(firstName: string, lastName: string) {
let params = {
TableName: 'MY_USERS_TABLE',
Item: {
id: uuid(),
firstName: firstName,
lastName: lastName
},
ReturnValues: 'ALL_OLD',
}
return Observable.fromPromise(database.put(params).promise())
}
Please refer to https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#putItem-property for more ReturnValues options.
@jeskew when I tried what you suggest I get the following error message:
ReturnValues can only be ALL_OLD or NONE
getting same error. Is there any solution available?
@jeskew when I tried what you suggest I get the following error message:
ReturnValues can only be ALL_OLD or NONE
Same issue. This is strange and confusing that Dynamodb PUT operation doesn't return any value.
I'm actually still having an issue with the DocumentClient Put method. With the ReturnValues set to ALL_OLD or NONE, the object returned is still empty.
@normanhuang
Can you provide an example of your code?
It says inside the documentation , The ReturnValues parameter is used by several DynamoDB operations; however, PutItem does not recognize any values other than NONE or ALL_OLD.
@normanhuang
Can you provide an example of your code?
@srchase Yes, see below:
var dynamodb = new AWS.DynamoDB.DocumentClient();
let params = {
TableName: 'Campaigns',
Item: {
CampaignId: uuid.v4(),
name: 'Fubar'
},
ReturnValues: 'ALL_OLD'
};
dynamodb.put(params, (err, data) => {
if (err) {
console.error('Error: ', JSON.stringify(err, null, 2));
} else {
console.log('Successfully retrieved item:', JSON.stringify(data));
}
});
Apologies. I figured it out. I was adding new items, for which the PutItem method and, by extension, the DocumentClient Put method returns empty because there were no older items to return. I tried replacing an existing item and the old item was returned.
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
@jeskew when I tried what you suggest I get the following error message:
ReturnValues can only be ALL_OLD or NONE