我正在尝试通过URL传递实体ID,然后获取该ID的实体。 这是我的代码。
// returns the common key for all users
func usersKey(c appengine.Context) *datastore.Key {
return datastore.NewKey(c, "User", "default_user", 0, nil)
}
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// ... validations bla bla
u := User{Email: email, Hash: hash, Joined: time.Now()}
key := datastore.NewIncompleteKey(c, "User", usersKey(c))
if _, err := datastore.Put(c, key, &u); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func ProfileHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
params := mux.Vars(r)
key := datastore.NewKey(c, "User", params["id"], 0, usersKey(c))
u := new(User)
if err := datastore.Get(c, key, u); err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
}
params["id"]
是正确的。但datastore.Get
始终返回错误:datastore: no such entity