为了账号安全,请及时绑定邮箱和手机立即绑定

从字符串创建 Google App Engine 数据存储区键

从字符串创建 Google App Engine 数据存储区键

Go
波斯汪 2021-08-23 17:56:05
我将实体“id”作为字符串传递给 URL,例如:..../foo/234565我想在以下查询中使用 id://...i hace some code that gets stringId from the URL, and I verified that it worksstringId = ....theKey, err := datastore.DecodeKey(stringId)q := datastore.NewQuery("Foo").Filter("__key__ =", theKey)我得到的错误:proto: can't skip unknown wire type 7 for datastore.Reference有没有一种简单的方法可以将 stringId 转换为“密钥”?
查看完整描述

1 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

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)


查看完整回答
反对 回复 2021-08-23
  • 1 回答
  • 0 关注
  • 177 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信