dso0139 2015-01-29 17:18
浏览 31
已采纳

Go中出现奇怪的数据存储错误,“种类是空字符串”

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条回答 默认 最新

  • dsp1836 2015-02-03 01:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断