branco
branco

Reputation: 154

Trying to put an entity in the datastore appears to put only a key and not the associated property I had in mind

This is where I try to insert the entity with the email property. In the browser U can see that db_success is received. In the datastore viewer I can see that the entity was inserted by way of 2 writes which is suspiciously low, and browsing to the entity I can see the entity kind, the entity key and the ID, but nothing else:

    c := appengine.NewContext(r)
    u := user.Current(c)

    if u == nil || !user.IsAdmin(c) {
        return
    }

    addrmv := r.FormValue("addrmv")
    user_email := r.FormValue("user_email")
    if addrmv == "add" {
        if user_email == "" {
            return
        }
        uemail := uemail_struct{
            user_email: user_email,
        }
        _,err :=    datastore.Put(c,datastore.NewIncompleteKey(c,"users",nil),&uemail)
        if err != nil {
            io.WriteString(w, "\"result\":\"db_error\"")
        } else {
            io.WriteString(w, "\"result\":\"db_success\"")
        }
    } else if addrmv == "rmv"{
        return
        //TODO expand
    }

The other piece of code is where I want to restrict user access based on email. I try to do a datastore query on the email property of the entity that I inserted previously and redirect authorized accounts to the intended page and unauthorized accounts back to the app entry url:

c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
    http.Redirect(w, r, "/", http.StatusSeeOther)
} else if user.IsAdmin(c) {
    io.WriteString(w, admins_choice_html)
} else {
    var emails []uemail_struct
    q := datastore.NewQuery("users").Filter("user_email =", u.Email)
    _,err := q.GetAll(c,&emails)
    if err != nil {
        http.Redirect(w, r, "/", http.StatusSeeOther)
    }
    if len(emails) == 0 {
        url, err := user.LogoutURL(c, "/")
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Header().Set("Location", url)
        w.WriteHeader(http.StatusFound)

    } else {
        http.Redirect(w, r, "/dist/index.html", http.StatusSeeOther)
    }
}

I am certain that my test data and functionality for adding user emails was ok. Meaning that I executed the first piece of code for the data [email protected] email address and executed the second piece of code for the same [email protected] email address. The only conclusion I can come to is that the datastore put in the first piece of code did not insert the required property user_email with value [email protected] in the datastore.

Upvotes: 0

Views: 72

Answers (1)

branco
branco

Reputation: 154

I found out that the problem was simply they way I was defining my structs. Instead of

type uemail_struct{
    user_email string
}

simply writing

type Uemail_struct{
    User_email string
}

fixes the program and now both adding new users and their login work.

Upvotes: 1

Related Questions