bson无法将值解码为nil值,使用bson:”omitempty”结构标签

huangapple go评论68阅读模式
英文:

bson cannot Decode to nil value with bson:"omitempty" struct tag

问题

我有一个结构体:

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"` // 在graphql中被省略
}

我调用了这段代码:

updateOption := options.FindOneAndUpdate().SetUpsert(true)

updateData := bson.M{"$set": *user}

filter := bson.M{"google_id": user.GoogleID}

// 更新后的用户将具有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,
}

但是我收到了这个错误:无法解码为nil值。这可能是因为指针为nil,但omitempty构建标签应该在这种情况下省略字段。为什么会失败呢?

英文:

I have a struct:

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
}

I'm calling this code:

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)

With the following user:

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,
}

but I'm getting this error saying: cannot Decode to nil value. This is probably because a pointer is nil, but the omitempty build tag should just omit the field in that case. Why would it fail to do so?

答案1

得分: 1

updatedUser是不能为nil的,你需要至少给它一个空的结构体以便解码。

英文:

updatedUser is what cannot be nil, you need to give it an empty struct at least to decode into.

答案2

得分: -1

似乎updateData := bson.M{"$set": *user}不是有效的字符串。你可以尝试updateData := bson.M{"$set": &user},这似乎可以工作。

英文:

Seems like updateData := bson.M{"$set": *user} is not valid string. You can try updateData := bson.M{"$set": &user} that seems to work.

huangapple
  • 本文由 发表于 2022年6月28日 01:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/72776154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定