gpt4 book ai didi

node.js - 将 getItem 与 DynamoDB 和 Node aws-sdk 库一起使用时出错

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:17 24 4
gpt4 key购买 nike

以下代码在尝试访问我的 AWS DynamoDB 表时会生成此错误消息。该表称为“客户”,该列称为“电子邮件”,值为“james@gmail.com”。我直接从此处提供的 AWS 示例中复制了此代码 https://github.com/awsdocs/aws-doc-sdk-examples 。我做错了什么吗?我在这上面浪费了好几天时间,尝试不同的事情:-(

任何帮助表示赞赏。谢谢,

错误消息

{"message":"Expected params.Key to be a map","code":"InvalidParameterType","time":"2019-03-13T23:05:59.231Z"}

NodeJs 代码

const express = require('express')
const app = express()
const port = 8080
var AWS = require('aws-sdk');

app.get('/test', (req, res) => {

// Set the region
AWS.config.update({region: 'eu-west-1'});

// Create DynamoDB document client
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var key = { email: 'james@gmail.com' };

ddb.getItem({
TableName: 'customers',
Key: key
}, function(err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});

});

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

DynamoDB 表

enter image description here

再次尝试我也收到此错误消息

{"message":"There were 20 validation errors:\n* InvalidParameterType: Expected params.Key['email'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '1' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '2' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '3' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '4' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '5' found in params.Key['email']\n* UnexpectedParameter: Unexpected key '6' found in 

更新

 var key = { email: 'james@gmail.com' };

ddb.getItem({
TableName: 'customers',
Key: key
}, function(err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});

更新

const express = require('express')
const app = express()
const port = 8080
var AWS = require('aws-sdk');

app.get('/test', (req, res) => {

AWS.config.update({region: 'eu-west-1'});
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var params = {
TableName: 'customers',
Key: {
'email': {S: 'james@gmail.com'}
},
};

ddb.getItem(params, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(data);
}
});

});

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

错误结果

{"message":"The provided key element does not match the schema","code":"ValidationException","time":"2019-03-14T19:26:13.602Z","requestId":"EIGKLLNEJR2DKSET6787JHQLC7VV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":46.10177725769697}

最佳答案

第二次编辑:我完全错过了您没有使用 DocumentClient (正如 Vladyslav Ulenko 指出的那样),因此为了完整性更新了下面的答案。

<小时/>

您有两个选择:

第一个选项使用带有 getItem 方法的 DynamoDB 服务对象,如下所示:

...
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var params = {
TableName: 'customers',
Key: {
'email': {S: 'james@gmail.com'} . // <-- pay attention to data type 'S'
},
};

ddb.getItem(params, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(data);
}
});

指定 DynamoDB 数据(属性值)类型 here (查找表编码/解码映射。

第二个选项是使用DocumentClient。在这种情况下,您不必担心编码(marshal)和取消编码(marshal)请求/响应数据(DynamoDB 值与 JavaScript 类型之间的转换)。在这种情况下,您的代码如下所示:

...

var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});


var params = {
TableName: 'customers',
Key: {'email': 'james@gmail.com'}
};

docClient.get(params, function (err, data) {
if (err) {
res.send(err);
} else {
res.send(data);
}
});
...
<小时/>

我建议您使用 DocumentClient 而不是 DynamoDB'getItem` 操作,因为它完全处理所有数据转换!

如果不这样做,您还需要处理响应数据类型,例如:

data.Items.forEach(function(element, index, array) {
console.log(element.FristName.S + " " + element.LastName.S);
});

不确定你是否愿意

<小时/>

第三次编辑:现在您已经完成了上述操作,您将收到错误:

“提供的关键元素与架构不匹配”

这意味着您使用的 key 与架构上定义的 key 不同,同样来自 documentation :

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

检查表详细信息中的键(示例) enter image description here

并确保将其中提到的所有 key 都放入 key 参数中

正如 Vladyslav 在其答案的评论部分中指出的那样,如果您需要使用不是主键的属性查找项目,则需要使用 scan操作而不是 getItem。您可以在 documentation 中找到示例

关于node.js - 将 getItem 与 DynamoDB 和 Node aws-sdk 库一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com