MIkCode
MIkCode

Reputation: 2835

datastore: invalid entity type on get

Hey im trying to get an entity by passing an id ,

But it`s look like im getting a nill pointer I tried to initialize the entity in couple of ways but the result is the same.

I'm trying to create my server as much as i can like this example to-do

what am im missing here ?

type UserManager struct {
    users []*Users
    user  *Users
}

func NewUserManager() *UserManager {
    return &UserManager{}
}

 func (userManager *UserManager) putUser(c appengine.Context, u *Users) (usreRes *Users, err error) {

key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "users", nil), u)
if err != nil {
    return nil, err
 }
c.Debugf("file key inserted :%#v", key)
return u, nil
}


func (userManager *UserManager) getUserById(userKey string, c appengine.Context) (usreRes *Users, err error) {

entity_id_int, err := strconv.ParseInt(userKey, 10, 64)

if err != nil {
    return userManager.user, err
}
k := datastore.NewKey(c, "users", "", entity_id_int, nil)

userRes := new(Users)
err = datastore.Get(c, k, &userRes)

//err = datastore.Get(c, k, &userManager.user)

if err != nil {
    return userManager.user, err
}

//return userManager.user,nil
return userRes, nil
}

Upvotes: 1

Views: 1561

Answers (1)

John Balvin Arias
John Balvin Arias

Reputation: 2886

It's because datastore.Get(c, k, "pointer") expects a pointer to struct as its third argument, and you are passing a pointer of a pointer **userRes

userRes := new(Users)

There you are creating a pointer to struct see

So you should do:

datastore.Get(c, k, userRes)

Without the ampersand, because with new keyword you are already creating a pointer

Upvotes: 2

Related Questions