首頁  >  文章  >  後端開發  >  在 Golang 中取得 Dynamodb 的 ValidationException

在 Golang 中取得 Dynamodb 的 ValidationException

王林
王林轉載
2024-02-14 21:30:101230瀏覽

在 Golang 中获取 Dynamodb 的 ValidationException

在Golang中,與Dynamodb進行互動時,有時可能會遇到ValidationException的錯誤。這個錯誤通常表示請求的資料不符合Dynamodb表的約束條件。在本文中,我們將透過php小編子墨的指導,介紹如何在Golang中取得Dynamodb的ValidationException錯誤,並提供解決方案,以便順利處理這類錯誤。無論您是初學者還是有一定經驗的開發者,本文都將對您有所幫助。讓我們一起來探索如何處理這個常見的錯誤吧!

問題內容

我做了一個這樣的模式~

type movie struct {
    year         int    `json:"year"`
    title        string `json:"title"`
    key          string `json:"userid"`
    email        string `json:"email"`
    bio          string `json:"bio"`
    number       int    `json:"phonenumber"`
    socialhandle string `json:"socialhandle"`
    onboarding   string `json:"username"`
    bankdetails  string `json:"bankdetails"`
    image        string `json:"image"`
    password     string `json:"password"`
    resume       string `json:"resume"`
    pincode      string `json:"pincode"`
}

這裡的 key 和 onboarding 分別是我的主要鍵和排序鍵。然後我就這樣加入了數據~

movie := movie{
    key:        "2323",
    onboarding: "the big new movie",
}

然後是我製作的東西的普通 marshalmap,並使用數據來獲取項目。

key, err := dynamodbattribute.MarshalMap(movie)
if err != nil {
    fmt.Println(err.Error())
    return
}

input := &dynamodb.GetItemInput{
    Key:       key,
    TableName: aws.String("tablename"),
}

result, err := svc.GetItem(input)
if err != nil {
    fmt.Println(err)
    fmt.Println(err.Error())
    return
}

奇怪的是,我使用相同的程式碼插入數據,幾乎沒有進行任何更改,但是在獲取數據時它顯示錯誤〜validationexception:提供的關鍵元素與架構不匹配

解決方法

此錯誤可能是由於在getitem 呼叫中發送非鍵屬性所引起的。當您使用 marshalmap 時,它會為鍵物件中的所有其他屬性包含一個 null 值。

您可以手動建構金鑰:

key: map[string]*dynamodb.attributevalue{
  "userid": {
    s: aws.string("2323"),
  },
  "username": {
    s: aws.string("the big new movie"),
  },
},

或將 omitempty 添加到結構字段,這會在這些屬性沒有值時從編組映射中排除它們:

type Movie struct {
    Year         int    `json:"year,omitempty"`
    Title        string `json:"title,omitempty"`
        [...]
}

以上是在 Golang 中取得 Dynamodb 的 ValidationException的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除