我正在为 NodeJS 使用 Vogels,DynamoDB 数据映射器。我正在查询 全局指数根据 Vogels 的文档。我所做的就是,创建了一个具有全局二级索引的模型,如下所示:
let MyModel = vogels.define('MyModel', {
hashKey: 'uuid',
timestamps: true,
updatedAt: 'updated_at',
createdAt: 'created_at',
schema: MyModelBaseSchema,
indexes : [{
hashKey : 'gameTitle', rangeKey : 'topScore', name : 'GameTitleIndex', type : 'global'
}]
});
并查询此索引
MyModel.query('game 1')
.usingIndex('GameTitleIndex')
.loadAll()
.select("COUNT");
运行任何测试时,它会显示异常
ValidationException: The table does not have the specified index: GameTitleIndex
根据文档,这就是我获取数据所需要做的全部工作。我在查询这个索引时有什么遗漏吗?
任何答案将不胜感激。
提前致谢。
最佳答案
– 如果您使用无服务器框架 无服务器动态数据库本地 和 无服务器离线 插件(用于本地测试)。
– 如果您是新创建的 本地二级索引 ,但下面显示的命令仍然没有显示在 LocalSecondaryIndexes
下配置 Node :
aws dynamodb describe-table --table-name YOUT_TABLE_NAME --endpoint-url http://localhost:8000
– 如果您收到错误 ValidationException: The table does not have the specified index: YOUR_INDEX_NAME
使用类似于下图的代码时: query(uid, id) {
const params = {
TableName: YOUR_TABLE_NAME,
IndexName: YOUR_LSI_NAME,
KeyConditionExpression: "#uid = :uid and #id = :id",
ExpressionAttributeNames: {
"#uid": "uid",
"#id": "id",
},
ExpressionAttributeValues: { ":uid": uid, ":id": id },
};
return DB.query(params).promise();
}
那么很可能你需要删除 本地 DynamoDB 数据库文件并使用 serverless offline start
重新启动服务命令。这将强制使用适当的 重新创建本地 DynamoDB 数据库文件本地二级索引 .要删除本地 DynamoDB 数据库文件,请执行以下操作:
serverless.yml
通过指定 DynamoDB 数据库的路径,如下所示:custom:
dynamodb:
stages:
- ${self:provider.stage}
start:
port: 8000
inMemory: true
migrate: true
dbPath: "./.db" # Make sure that folder exists!
./.db
并删除文件 shared-local-instance.db
(这是一个模仿 DynamoDB 的 SQLite 数据库:0)。 结果在下一次开始时使用命令
serverless offline start
最新 将创建本地 DynamoDB 数据库文件。
关于node.js - 验证异常 : The table does not have the specified index: GameTitleIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62278154/