如何修复“提供的键元素与模式不匹配”错误?

huangapple go评论159阅读模式
英文:

How to fix 'The provided key element does not match the schema' error?

问题

以下是您的代码的翻译部分:

  1. exports.handler = async (event) => {
  2. try {
  3. const IDs =
  4. [
  5. {"profileID": {"S": "my_first_id"}},
  6. {"profileID": {"S": "my_second_id"}}
  7. ]
  8. const postsResult = await db.batchGetItem({
  9. RequestItems: {
  10. 'Feed': {
  11. Keys: IDs
  12. }
  13. }
  14. }).promise();
  15. return {
  16. statusCode: 200,
  17. body: JSON.stringify(postsResult),
  18. };
  19. } catch (err) {
  20. console.log(err)
  21. return { error: err }
  22. }
  23. }

错误信息:

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

架构信息:

  1. type Feed @model @auth(rules: [{allow: private}]) {
  2. profileID: String @index(name: "feedByProfileID")
  3. type: String
  4. postID: String
  5. shareID: String
  6. likeID: String
  7. commentID: String
  8. replyID: String
  9. }
英文:

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:

  1. exports.handler = async (event) => {
  2. try {
  3. const IDs =
  4. [
  5. {"profileID": {"S": "my_first_id"}},
  6. {"profileID": {"S": "my_second_id"}}
  7. ]
  8. const postsResult = await db.batchGetItem({
  9. RequestItems: {
  10. 'Feed': {
  11. Keys: IDs
  12. }
  13. }
  14. }).promise();
  15. return {
  16. statusCode: 200,
  17. body: JSON.stringify(postsResult),
  18. };
  19. } catch (err) {
  20. console.log(err)
  21. return { error: err }
  22. }
  23. }

But I get this error:

> The provided key element does not match the schema

And this is the schema:

  1. type Feed @model @auth(rules: [{allow: private}]){
  2. profileID: String @index(name: "feedByProfileID")
  3. type: String
  4. postID: String
  5. shareID: String
  6. likeID: String
  7. commentID: String
  8. replyID: String
  9. }

答案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:

  1. Send multiple Query calls
  2. Send a PartiQL ExecuteStatement call
  1. // Declare function
  2. const exStatement = statement => {
  3. db.executeStatement({
  4. Statement: statement
  5. })
  6. .promise() // Promise
  7. .then(res => {
  8. console.log(JSON.stringify(res))
  9. })
  10. .catch(err => console.log(err))
  11. }
  12. // Call function
  13. 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:

  1. Send multiple Query calls
  2. Send a PartiQL ExecuteStatement call
  1. // Declare function
  2. const exStatement = statement => {
  3. db.executeStatement({
  4. Statement: statement
  5. })
  6. .promise() // Promise
  7. .then(res => {
  8. console.log(JSON.stringify(res))
  9. })
  10. .catch(err => console.log(err))
  11. }
  12. //Call function
  13. exStatement(`SELECT * from "Feed"."feedByProfileID" where profileID IN ['my_first_id','my_second_id']`) // Up to 50 PK's
  14. </details>

huangapple
  • 本文由 发表于 2023年7月4日 20:48:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定