gpt4 book ai didi

javascript - 发电机 : Query Incorrect operand type

转载 作者:行者123 更新时间:2023-11-29 19:04:54 24 4
gpt4 key购买 nike

我正在尝试读取 DynamoDB 表中高于特定值的所有值。我将主分区键设置为一个名为 Project_ID 的数字。我正在运行查询以查看高于某个 ID 的所有值 - 主要是为了测试功能,但是在运行代码时出现错误。

代码:

    var params = {
TableName : document.getElementById("tableName").value,
KeyConditionExpression: "Project_ID > :v1",
"ExpressionAttributeValues": {
":v1": {"N": "0"}
}
};

docClient.query(params, function(err, data) {
if (err) {
document.getElementById('textarea').innerHTML += "Unable to query. Error: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
data.Items.forEach(function(project) {
//JSON.stringify(project);
document.getElementById('textarea').innerHTML += "\n" + project.Project_Name + ": " + project.Project_Ref;
});

}
});

输出

    `Unable to query. Error: {
"message": "Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: >, operand type: M",
"code": "ValidationException",
"time": "2017-04-28T10:52:31.381Z",`

最佳答案

实际上,你这里有两个错误:

  • 事实上,第一个是您尝试在主分区键上应用不是 = 的运算符。 但是,这不是您在此处收到错误消息的原因!

  • 在您的错误消息中,M 类型用于Mapping(或Dictionary),运算符 > 无法应用。实际上,您正在使用 SDK 的 DocumentClient 抽象,并且如 documentation 中所指定的那样。 :

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values.

这意味着在使用文档客户端进行调用时,您应该具有以下查询参数:

var params = {
TableName : document.getElementById("tableName").value,
KeyConditionExpression: "Project_ID = :v1", //I modified this to fix the first point
ExpressionAttributeValues: {
":v1": 0 //Not a dictionary!
}
};

代替:

var params = {
TableName : document.getElementById("tableName").value,
KeyConditionExpression: "Project_ID = :v1", //I modified this to fix the first point
ExpressionAttributeValues: {
":v1": {"N": "0"} //This is an attribute value !
}
};

关于javascript - 发电机 : Query Incorrect operand type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43678636/

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