DynamoDB ConditionalExpression 无法识别属性名称或值

DynamoDB ConditionalExpression not recognizing Attribute Names or Values

我正在使用 NodeJS 的 aws-sdk 并尝试进行更新,以便在项目不存在时抛出错误。我正在使用表达式 API 而不是旧表达式。这是我不适合我的人为示例。

client.update({
    TableName: 'User', 
    Key: {'_id': '10'}, 
    UpdateExpression: 'SET username = :user, password = :pword', 
    ConditionalExpression: 'attribute_exists(#idKey) AND #idKey = :idVal', 
    ExpressionAttributeNames: {
        '#idKey': '_id'
    }, 
    ExpressionAttributeValues: {
        ':idVal': '10', 
        ':user': 'user10', 
        ':pword': 'password10'
    }}, function(err, data){
        if(err) console.log(err); 
        else console.log(data);
});

ValidationException:表达式中未使用的 ExpressionAttributeNames 中提供的值:键:{#idKey}

我尝试了各种其他 ConditionalExpressions,既使用属性名称又将实际值插入到表达式中。我开始认为这是一个错误。将遗留的 Expected->Exists 与遗留的 AttributeUpdate 一起使用是可行的,但我无法使用表达式演示此功能。

您已经使用 UpdateItemRequest 的 Key 参数缩小到 _id=10 的特定项目。如果某个项目不存在,则无法将 UpdateItem 调用设置为键的特定值。因此,只需要 ConditionExpression 中的attribute_exists(#idKey)

以下代码引发了您想要的行为(我不得不将 table 名称更改为 Images,将主键更改为 Id 以匹配 DynamoDB Local Shell 教程的内容。

var params = {
    TableName: 'Image',
    Key: { // The primary key of the item (a map of attribute name to AttributeValue)
        '_id': 'dynamodb.png'
    },
    UpdateExpression: 'SET username = :user, password = :pword',
    ConditionExpression: 'attribute_exists(#id)',
    ExpressionAttributeValues: {
        ':user': 'user10', 
        ':pword': 'password10'
    },
    ExpressionAttributeNames: {
        '#id': '_id'
    },
    ReturnValues: 'ALL_NEW'
};
docClient.update(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

提醒一下,请不要post这里有任何真实的密码数据:)