Use below codes, I successfully update an item
Then I add more attributes:
const params = {
TableName: process.env.DYNAMODB_TABLE,
Key: {
id: event.pathParameters.id,
},
ExpressionAttributeNames: {
'#user_name': 'name',
},
ExpressionAttributeValues: {
':name': data.name,
':email': data.email,
':username': data.username,
':password': data.password,
':checked': data.checked,
':updatedAt': timestamp,
},
UpdateExpression: 'SET #user_name = :name, email = :email, username = :username, password = :password, checked = :checked, updatedAt = :updatedAt',
ReturnValues: 'ALL_NEW',
};
Above codes work fine if I feed all attributes.
$ cat test/user-1.json
{
"name": "Bob",
"email": "bob@example.com",
"username": "bob",
"password": "adfdsfdsf",
"checked": false
}
But if I only want to update part of them, since I needn’t update email and password every time, I got error Couldn't fetch the user item.
$ cat test/user-1.json
{
"name": "Bob",
"username": "bob-1",
"checked": false
}
$ curl -X PUT ${url}/${id} --data '@test/user-1.json'
Couldn't fetch the user item.
So how to change the code that I don’t have to update all attributes.