frederix
frederix

Reputation: 1862

Update entity from appengine .GetAll and save to datastore

I have a model:

type UserProfile struct {
UserID         string    `datastore:"user_id" json:"user_id,omitempty"`
Username       string    `datastore:"username" json:"username,omitempty"`
StripUsername  string    `datastore:"strip_username" json:"strip_username,omitempty"`
Email          string    `datastore:"email" json:"email,omitempty"`
LoginType      string    `datastore:"login_type" json:"login_type,omitempty"`
CurrentSession string    `datastore:"current_session" json:"current_session,omitempty"`
FBAcessToken   string    `datastore:"fb_access_token" json:"fb_access_token,omitempty"`
Created        time.Time `datastore:"created" json:"-"`
}

And I perform a .GetAll to populate it:

// Skip a few steps here

var userProfiles []UserProfile
q.GetAll(c, &userProfiles)

Say I want to modify one of those entities:

userProfile[0].Email = "[email protected]"

I know I want to Put that entity like so:

k = datastore.Put(c, k, userProfile[0])

How do I get that initial key from userProfile[0] to call Put with?

Upvotes: 2

Views: 98

Answers (1)

Thundercat
Thundercat

Reputation: 121139

GetAll returns the keys:

var userProfiles []UserProfile
keys, err := q.GetAll(c, &userProfiles)
if err != nil {
    // handle error
}

Update entities using the keys returned from GetAll:

userProfile[0].Email = "[email protected]"
_, err = datastore.Put(c, keys[0], userProfile[0])
if err != nil {
    // handle error
}

Upvotes: 3

Related Questions