我有一个结构:
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,
}但我得到了这样一个错误:无法解码为零值。这可能是因为指针为零,但在这种情况下,省略的build标记应该只是省略字段。为何不这样做呢?
发布于 2022-06-27 17:59:07
updatedUser是不可能为零的,您需要给它一个空的结构,至少要解码。
发布于 2022-06-28 09:11:30
似乎updateData := bson.M{"$set": *user}不是有效的字符串。您可以尝试似乎有效的updateData := bson.M{"$set": &user}。
https://stackoverflow.com/questions/72776154
复制相似问题