无法将我的结构体放入数据存储(golang)

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

Failed to put my struct to datastore (golang)

问题

这是我的结构:

type AreaPrerequisite struct {
    SideQuestId   int // 任务ID
    SideQuestProg int // 进度
}

type AreaInfo struct {
    Id                int              `datastore:""`
    Name              string           `datastore:",noindex"`
    ActionPoint       int              `datastore:",noindex"`
    Prerequisite      AreaPrerequisite `datastore:",noindex"`

    // 忽略的字段:
    DsMonsters        []byte           `datastore:"-"`
    DsStages          []byte           `datastore:"-"`
    Monsters          AreaMonsters     `datastore:"-"`
    Stages            []*StageEntry    `datastore:"-"`
}

这是我的put()调用:

key := datastore.NewKey(c, "Area", "", int64(pArea.Id), nil)
_, err := datastore.Put(c, key, *pArea)
if err != nil {
    return err
}

当尝试将数据放入Datastore时,它给我返回以下错误:

datastore: invalid entity type

我查看了文档:
https://developers.google.com/appengine/docs/go/datastore/reference

datastore:"-" 应该标记一些Datastore不支持的字段,让其被忽略。
不知道为什么会失败。

英文:

here is my struct:

type AreaPrerequisite struct {
	SideQuestId   int // 
	SideQuestProg int // progress
}

type AreaInfo struct {
	Id                int              `datastore:""`
	Name              string           `datastore:",noindex"`
	ActionPoint       int              `datastore:",noindex"`
	Prerequisite      AreaPrerequisite `datastore:",noindex"`

    // ignored:
	DsMonsters        []byte           `datastore:"-"`
	DsStages          []byte           `datastore:"-"`
	Monsters          AreaMonsters     `datastore:"-"`
	Stages            []*StageEntry    `datastore:"-"`
}

and my put() call:

key := datastore.NewKey(c, "Area", "", int64(pArea.Id), nil)
_, err := datastore.Put(c, key, *pArea)
if err != nil {
	return err
}

It gives me the following error when try to put to DS:

datastore: invalid entity type

I checked the doc:
https://developers.google.com/appengine/docs/go/datastore/reference

datastore:"-" should mark some non-supported fields ignored by datastore.
Don't know why it is failing.

答案1

得分: 16

我发现我不小心将 * 添加到 pArea 作为 put() 的参数,所以它传递的是一个值而不是指针,导致实体类型错误。

英文:

I found that I accidentally added * to pArea as arg to put() so it is passing a value instead of pointer, causing invalid entity type error.

答案2

得分: 4

我也遇到了同样的问题,在我的情况下,我没有在要放置的实体之前放置&

key := datastore.NewKey(c, "Area", "", int64(pArea.Id), nil)
_, err := datastore.Put(c, key, &pArea)
if err != nil {
    return err
}

注意在&pArea之前的**&**。

英文:

I've also run into that same problem, in my case I didn't put & before the entity to be putted.

key := datastore.NewKey(c, "Area", "", int64(pArea.Id), nil)
_, err := datastore.Put(c, key, &pArea)
if err != nil {
    return err
}

Notice the & before &pArea

huangapple
  • 本文由 发表于 2013年8月17日 00:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/18277632.html
匿名

发表评论

匿名网友

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

确定