英文:
Weird datastore error in Go, "The kind is the empty string"
问题
我最近在进行一个简单的datastore.GetAll()请求时遇到了一个我从未见过的错误。我无法弄清楚它的含义,也无法找到任何关于这个错误消息的文档或通过谷歌搜索错误消息来获得帮助。
以下是我的代码:
type MyUnderlyingStruct struct {
ApplyTo *datastore.Key
ApplyFrom *datastore.Key
Amount float64
LocationKey *datastore.Key
DepartmentKey *datastore.Key
SubjectAreaKey *datastore.Key
}
type MyStruct []MyUnderlyingStruct
//在出现错误的情况下,someKey是一个有效的、完整的Key值,与我们查询的类型不同,并且实际上在我的数据存储中有一个与此查询匹配的实体
func (x *MyStruct) Load(w http.ResponseWriter, r *http.Request, someKey *datastore.Key) (error) {
c := appengine.NewContext(r)
q := datastore.NewQuery("MyUnderlyingStruct_KindName").Order("-Amount")
if someKey != nil { q = q.Filter("ApplyTo=", someKey) }
keys, err := q.GetAll(c,x)
if _, ok := err.(*datastore.ErrFieldMismatch); ok { err = nil }
if err != nil && err != datastore.Done {return err}
return nil
}
这会返回以下错误:
API error 1 (datastore_v3: BAD_REQUEST): The kind is the empty string.
有人能告诉我为什么会出现这个错误,或者它试图告诉我什么吗?
英文:
I am recently getting an error that I have never seen before when making a simple datastore.GetAll() request. I can't figure out what it means and I can't find any documentation with the error message or any help from Googleing the error message.
Here's my code:
type MyUnderlyingStruct struct {
ApplyTo *datastore.Key
ApplyFrom *datastore.Key
Amount float64
LocationKey *datastore.Key
DepartmentKey *datastore.Key
SubjectAreaKey *datastore.Key
}
type MyStruct []MyUnderlyingStruct
//In the case where I get the error someKey is a valid, complete Key value
// of a different kind that what we are querying for and there is actually
// an entity in my datastore that matches this query
func (x *MyStruct) Load(w http.ResponseWriter, r *http.Request, someKey *datastore.Key) (error) {
c := appengine.NewContext(r)
q := datastore.NewQuery("MyUnderlyingStruct_KindName").Order("-Amount")
if someKey != nil { q = q.Filter("ApplyTo=", someKey) }
keys, err := q.GetAll(c,x)
if _, ok := err.(*datastore.ErrFieldMismatch); ok { err = nil }
if err != nil && err != datastore.Done {return err}
return nil
}
Which returns this error:
API error 1 (datastore_v3: BAD_REQUEST): The kind is the empty string.
Can anyone tell me why I am getting this error, or what it is trying to tell me?
答案1
得分: 1
在初步查看您的问题时(因为我不熟悉Google的Datastore API),我觉得问题是使用new
关键字进行零内存初始化导致的。
当使用关键字创建一个结构体时,如果没有为字段分配起始值,将默认为0。当映射为字符串时,它是""(空字符串)。Go实际上为您抛出了一个非常有帮助的错误。
正如您指出的,您使用了Mykey := new(datastore.Key)
。感谢您的慷慨,这可以作为未来用户的答案。
英文:
Looking at your issue on the first glance (because I am not familiar with Google's datastore API), it seems to me the problem is a result of zeroed-memory initialization using new
keyword.
When a struct is created with the keyword without assigning starting values for the fields, 0's are given as default. When mapped to string, it's "" (empty). Go actually threw a very helpful error for you.
As you have pointed out, you have used Mykey := new(datastore.Key)
. Thanks for your generosity and this can serve as answer for future users.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论