英文:
How to fix 'The provided key element does not match the schema' error?
问题
以下是您的代码的翻译部分:
exports.handler = async (event) => {
try {
const IDs =
[
{"profileID": {"S": "my_first_id"}},
{"profileID": {"S": "my_second_id"}}
]
const postsResult = await db.batchGetItem({
RequestItems: {
'Feed': {
Keys: IDs
}
}
}).promise();
return {
statusCode: 200,
body: JSON.stringify(postsResult),
};
} catch (err) {
console.log(err)
return { error: err }
}
}
错误信息:
提供的键元素与架构不匹配
架构信息:
type Feed @model @auth(rules: [{allow: private}]) {
profileID: String @index(name: "feedByProfileID")
type: String
postID: String
shareID: String
likeID: String
commentID: String
replyID: String
}
英文:
I am using DynamoDB for my application and I need to get some items that match one of the keys in an array, this is my code:
exports.handler = async (event) => {
try {
const IDs =
[
{"profileID": {"S": "my_first_id"}},
{"profileID": {"S": "my_second_id"}}
]
const postsResult = await db.batchGetItem({
RequestItems: {
'Feed': {
Keys: IDs
}
}
}).promise();
return {
statusCode: 200,
body: JSON.stringify(postsResult),
};
} catch (err) {
console.log(err)
return { error: err }
}
}
But I get this error:
> The provided key element does not match the schema
And this is the schema:
type Feed @model @auth(rules: [{allow: private}]){
profileID: String @index(name: "feedByProfileID")
type: String
postID: String
shareID: String
likeID: String
commentID: String
replyID: String
}
答案1
得分: 0
My assumption is you are trying to do a BatchGetItem
on a Global Secondary Index (GSI) which is not possible, this is because items in a GSI are not required to be unique, however, they are required to be unique for GetItem
and BatchGetItem
.
You can do one of 2 things:
- Send multiple
Query
calls - Send a PartiQL
ExecuteStatement
call
// Declare function
const exStatement = statement => {
db.executeStatement({
Statement: statement
})
.promise() // Promise
.then(res => {
console.log(JSON.stringify(res))
})
.catch(err => console.log(err))
}
// Call function
exStatement(`SELECT * from "Feed"."feedByProfileID" where profileID IN ['my_first_id','my_second_id']`) // Up to 50 PK's
英文:
My assumption is you are trying to do a BatchGetItem
on a Global Secondary Index (GSI) which is not possible, this is because items in a GSI are not required to be unique, however, they are required to be unique for GetItem
and BatchGetItem
.
You can do one of 2 things:
- Send multiple
Query
calls - Send a PartiQL
ExecuteStatement
call
// Declare function
const exStatement = statement => {
db.executeStatement({
Statement: statement
})
.promise() // Promise
.then(res => {
console.log(JSON.stringify(res))
})
.catch(err => console.log(err))
}
//Call function
exStatement(`SELECT * from "Feed"."feedByProfileID" where profileID IN ['my_first_id','my_second_id']`) // Up to 50 PK's
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论