Steven Lu
Steven Lu

Reputation: 2180

Querying with an array of arbitrary keys on Google datastore in Golang

A continuation from this question:

Doing a "IN Array" query on google app engine datastore with golang

Right now, I am following the suggestion from the previous question on querying with an array of keys/ids ids []int64. These IDs may or may not actually exist (they have been deleted, but the reference on other instances have not been removed).

My method of trying to obtain these instances looks like so:

var keys []*datastore.Key

for _, id := range ids {
    keys = append(keys, datastore.NewKey(c, "Category", "", id, nil))
}

categories := make([]Category, len(keys))
err := datastore.GetMulti(c, keys, categories)
if err != nil {
    return nil, err
}

for i := 0; i < len(categories); i++ {
    categories[i].Id = keys[i].IntID()
}

However, it errors out throwing me:

datastore: no such entity

I could on the other hand grab each one individually, but is there a more efficient way to approach this?

Upvotes: 1

Views: 350

Answers (1)

Logiraptor
Logiraptor

Reputation: 1528

You need to type assert the error to an appengine.MultiError. This way you can get access to the errors for an individual entity.

if me, ok := err.(appengine.MultiError); ok {
    for i, e := range me {
        // e != nil if entity i failed
    }
} else {
   // something else went wrong (timeout, etc).
}

See the docs for MultiError here

Upvotes: 2

Related Questions