在請求中,我可能會或可能不會有一個「電子郵件」字段。如何更新DynamoDB基於屬性的關上的屬性值?
在我的DynamoDB更新調用中,我想要構造一個UpdateExpression,使其根據請求的值更新「email」字段,但如果找不到,則使用該字段的前一個值。
它正確地更新,當我把「電子郵件」的要求,但如果我不這樣做,它只是更新領域的文字串,userProfile.email,而不是路徑,userProfile.email
controller.js:
async function updateUserProfile(ctx) {
if (ctx.params && ctx.params.userId) {
try {
const data = await db('update')({
TableName: TABLE_NAMES.USERS,
Key: {
userId: ctx.params.userId,
},
ExpressionAttributeValues: {
':email': ctx.request.body.email || 'userProfile.email' || null,
},
UpdateExpression:
'set userProfile.email = :email,
});
ctx.status = 200;
ctx.body = data;
} catch (error) {
ctx.throw(400, error);
}
}
}
如果我的請求主體看起來是這樣的:
{ "email": "newEmail" }
然後將更新用戶項目看起來就像是這樣的:
{ "userProfile": { "email": "newEmail" }
其目的,但如果我不包括在請求體「電子郵件」,更新後的用戶項目看起來是這樣的:
{ "userProfile": { "email": "userProfile.email" }
看來UpdateExpression字面上採取串而不是將其轉換爲路徑。我大概可以用ConditionExpression,但也有其他領域比「電子郵件」,他們也需要自己的ConditionExpression,我只能有一個處於通話狀態。
如何讓我看到它的價值作爲路徑,或有另一種方式,我可以與以前的屬性值更新?