通过ID获取实体返回数据存储区:没有这样的实体'

时间:2014-05-22 09:28:20

标签: google-app-engine go google-cloud-datastore

我正在尝试通过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

0 个答案:

没有答案