使用 Golang 操作 Dynamodb 时遇到了 ValidationException。

0

【以下的问题经过翻译处理】 我已经创建了这样的模式~

    type UserDetails struct {
	UserId       string `json:"userId,omitempty"`
	FirstName    string `json:"firstName,omitempty"`
	LastName     string `json:"lastName,omitempty"`
	Username     string `json:"username,omitempty"`
	HandleName   string `json:"handlename,omitempty"`
	Email        string `json:"email,omitempty"`
	Bio          string `json:"bio,omitempty"`
	Number       int    `json:"phoneNumber,omitempty"`
	SocialHandle string `json:"socialHandle,omitempty"`
	BankDetails  string `json:"bankDetails,omitempty"`
	Image        string `json:"image,omitempty"`
	Password     string `json:"password,omitempty"`
	Resume       string `json:"resume,omitempty"`
	Pincode      int    `json:"pinCode,omitempty"`
	Onboarding   string `json:"onboarding,omitempty"`
}

这里的Key和onboarding分别是我的主要和排序键。然后我加入了如下数据~

movie := Movie{
	UserId:        "2323",
	Username: "Tdae",
}

然后是我所做的普通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:提供的键元素与模式不匹配。

profile picture
EXPERTE
gefragt vor 8 Monaten37 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 这个错误很可能是在GetItem调用中发送了非主键属性导致的。当使用MarshalMap时,它会在键对象中包括所有其他属性的空值。

你可以手动构建键,例如:

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"`
        [...]
}

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen