我有一个结构:
type User struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Username *string `json:"username" bson:"username,omitempty"`
FirstName *string `json:"firstName" bson:"first_name,omitempty"`
LastName *string `json:"lastName" bson:"last_name,omitempty"`
Email *string `json:"email" bson:"email,omitempty"`
GoogleID *string `json:"googleID" bson:"google_id,omitempty"`
PageURLs []string `json:"pageURLs" bson:"pages"`
Schema int `json:"-" bson:"schema"` // omitted from graphql
}
我正在调用这段代码:
updateOption := options.FindOneAndUpdate().SetUpsert(true)
updateData := bson.M{"$set": *user}
filter := bson.M{"google_id": user.GoogleID}
// updated user will have Id
err = findOneUserAndUpdate(context.TODO(), filter, updateData, updateOption).Decode(updatedUser)
使用以下用户:
var testFirstname = "first"
var testLastname = "last"
var testEmail = "test@gmail.com"
var testGoogleID = "abc123"
testUser = &model.User{
FirstName: &testFirstname,
LastName: &testLastname,
Email: &testEmail,
GoogleID: &testGoogleID,
PageURLs: []string{},
Schema: 1,
}
但我收到此错误消息:无法解码为零值。这可能是因为指针为零,但在这种情况下,omitempty 构建标记应该只省略该字段。为什么会失败呢?
冉冉说
守着星空守着你
相关分类