英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论