dongqishun6409 2014-10-27 17:56
浏览 61
已采纳

通过字符串制作Google App Engine数据存储区密钥

I'm passing an entities "id" as a string on a URL, for example:

..../foo/234565

I want to use the id in the following query:

//...i hace some code that gets stringId from the URL, and I verified that it works
stringId = ....
theKey, err := datastore.DecodeKey(stringId)
q := datastore.NewQuery("Foo").Filter("__key__ =", theKey)

The error I'm getting:

proto: can't skip unknown wire type 7 for datastore.Reference

Is there a simple way to convert stringId into a "Key"?

  • 写回答

1条回答 默认 最新

  • dongtu1357 2014-10-27 18:44
    关注
    c := appengine.NewContext(r)
    //Retrieve the entity ID from the submitted form. Convert to an int64
    entity_id := r.FormValue("entity_id")
    entity_id_int, err := strconv.ParseInt(entity_id, 10, 64) 
    if err != nil {
      fmt.Fprint(w, "Unable to parse key")
      return;
    }
    //We manufacture a datastore key based on the Kind and the 
    //entity ID (passed to us via the HTTP request parameter.
    key := datastore.NewKey(c, kind, "", entity_id_int, nil)
    //Load the Entity this key represents.
    //We have to state the variable first, then conduct the Get operation 
    //so the datastore understands the struct representing the entity.
    var entity CustomStruct
    datastore.Get(c, key, &entity)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题