登录
首页 >  Golang >  Go问答

处理 Golang 中的 DynamoDB ValidationException

来源:stackoverflow

时间:2024-02-12 09:00:22 249浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《处理 Golang 中的 DynamoDB ValidationException》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

我做了一个这样的模式~

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学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>