Go 中出现奇怪的数据存储错误,"The kind is the empty string"

Weird datastore error in Go, "The kind is the empty string"

我最近在发出简单的 datastore.GetAll() 请求时遇到了以前从未见过的错误。我不明白这是什么意思,也找不到任何包含错误消息的文档或 Google 搜索错误消息的任何帮助。

这是我的代码:

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
}

其中returns这个错误:

API error 1 (datastore_v3: BAD_REQUEST): The kind is the empty string.

谁能告诉我为什么会出现此错误,或者它想告诉我什么?

乍一看你的问题(因为我不熟悉 Google 的数据存储 API),在我看来问题是使用 zeroed-memory 初始化的结果new 关键字。

当使用关键字创建结构时没有为字段分配起始值,默认值为 0。当映射到字符串时,它是“”(空)。 Go 实际上为您抛出了一个非常有用的错误。

正如您所指出的,您已经使用了Mykey := new(datastore.Key)。感谢您的慷慨,这可以作为未来用户的答案。