ValidationException:提供的键元素与架构不匹配 [英] ValidationException: The provided key element does not match the schema

查看:116
本文介绍了ValidationException:提供的键元素与架构不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DynamoDB中创建了一个表'user_info',它具有一个主哈希键'user_id'(String),没有范围键.然后,我创建了2个AWS lambda函数来插入和查询项目.我可以在表中插入项目,但是当我查询表时,它返回:

I created a table 'user_info' in DynamoDB with one primary hash key 'user_id'(String), no range key. Then I created 2 AWS lambda functions to insert and query the items. I can insert items into the table, but when I query the table, it returns:

ValidationException:提供的键元素与 模式.

ValidationException: The provided key element does not match the schema.

我的查询功能:

var params = {
   Key: {
       user_id:{
           S: "usr1@s.com"
       }
   },
    TableName: 'user_info',
    ProjectionExpression: 'password'
};

dynamodb.getItem(params,
 function(err, data) {
    if (err) {
        console.log("get item err." + err);
        context.done('error','getting item from dynamodb failed: '+err);
    }
    else {
        console.log('great success: '+JSON.stringify(data, null, '  '));
        context.succeed('created user ' + event.user_id + ' successfully.');
    }
});

我不断收到此异常:

ValidationException: The provided key element does not match the schema

1)我只有一个哈希主键.

1) I have only one hash primary key.

2)user_id被定义为String.我真的不知道为什么会出现不匹配错误.

2)user_id is defined as String. I really don't know why there is a mismatch error.

推荐答案

为进一步说明这种情况的发生原因,您最初使用的是

To clarify further on why this is happening, you were originally using the DynamoDB Document Client, which eliminates to need to explicitly label your attributes as "String" (S) or "Number" (N) etc. Therefore, your original code would have worked with

var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

var params = {
Key: {
   user_id: "usr1@s.com"
},
TableName: 'user_info',
ProjectionExpression: 'password'
};

注意,包裹"user_id"值的"S"已从上述代码中删除.稍后,您通过使用"aws-sdk"切换回低级javascript sdk,因此带有"S"标签的代码最终可以正常工作.

Note the "S" wrapping the value of "user_id" is removed from the above code. Later on, you switched back to the low-level javascript sdk by using 'aws-sdk', so your code with "S" label ended up working.

这篇关于ValidationException:提供的键元素与架构不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆