0

使用,我成功地更新项目更新几个属性在DynamoDB下面的代码

https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/todos/update.js#L22-L37

const params = { 
    TableName: process.env.DYNAMODB_TABLE, 
    Key: { 
    id: event.pathParameters.id, 
    }, 
    ExpressionAttributeNames: { 
    '#todo_text': 'text', 
    }, 
    ExpressionAttributeValues: { 
    ':text': data.text, 
    ':checked': data.checked, 
    ':updatedAt': timestamp, 
    }, 
    UpdateExpression: 'SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt', 
    ReturnValues: 'ALL_NEW', 
}; 

然后我添加更多属性:

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', 
    }; 

上面的代码工作,如果我喂所有属性。

$ cat test/user-1.json 
{ 
    "name": "Bob", 
    "email": "[email protected]", 
    "username": "bob", 
    "password": "adfdsfdsf", 
    "checked": false 
} 

但如果我只是想更新他们的一部分,因为我不需要每次更新电子邮件地址和密码,我得到了错误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. 

那么如何更改代码,我不不必更新所有属性。

回答

0

由于data.email和data.password未定义,Update API调用将不起作用。

我会建议在不需要更新这些属性时更改UpdateExpression。

相关问题