我正在使用Dynamoose来简化我在 node.js 应用程序中与 DynamoDB 的交互。我正在尝试使用 Dynamoose 的 Model.query 函数编写一个查询,该函数将使用索引搜索表,但似乎 Dynamoose 不包括处理查询所需的所有信息,我不确定我是什么做错了。
架构如下所示:
const UserSchema = new dynamoose.Schema({
"user_id": {
"hashKey": true,
"type": String
},
"email": {
"type": String,
"index": {
"global": true,
"name": "email-index"
}
},
"first_name": {
"type": String,
"index": {
"global": true,
"name": "first_name-index"
}
},
"last_name": {
"type": String,
"index": {
"global": true,
"name": "last_name-index"
}
}
)
module.exports = dynamoose.model(config.usersTable, UserSchema)
我希望能够通过电子邮件地址搜索用户,所以我正在编写一个如下所示的查询:
Users.query("email").contains(query.email)
.using("email-index")
.all()
.exec()
.then( results => {
res.status(200).json(results)
}).catch( err => {
res.status(500).send("Error searching for users: " + err)
})
当我尝试执行此查询时,我收到以下错误:
Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
使用 Dynamoose 调试输出,我可以看到查询最终看起来像这样:
aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
"#a0": "email"
},
"ExpressionAttributeValues": {
":v0": {
"S": "mel"
}
},
"TableName": "user_qa",
"IndexName": "email-index"
}
我注意到发送到 DynamoDB 的实际查询不包含 KeyConditions 或 KeyConditionExpression,如错误消息所示。我做错了什么会阻止正确编写此查询,以便它针对我为此表添加的全局二级索引执行查询?