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

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

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:

  1. Send multiple Query calls
  2. 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:

  1. Send multiple Query calls
  2. 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>



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:

确定