How do I query an appengine datastore with an emai

2019-06-08 07:35发布

I am trying to lookup users in appengine datastore using their email address. I'm using Go.

This code finds no users.

var users []entity.User
q := datastore.NewQuery("users").Filter("AccountEmail =", "email@address.com")
_, err := q.GetAll(c, &users)

If I change the query to lookup the user with the "Id" property instead it works fine.

var users []entity.User
q := datastore.NewQuery("users").Filter("Id", "185804764220139124118")
_, err := q.GetAll(c, &users)

I've confirmed the property name and value for "AccountEmail" is correct. "AccountEmail" It is indexed too.

Is there some special formatting that needs to be done with an email address to get the query to work?

1条回答
疯言疯语
2楼-- · 2019-06-08 08:04

In order to find the user by email (AccountEmail), all of the following conditions must be true. Please check and ensure each "test" passes:

  • An entity with property name AccountEmail must exists. Don't forget that the property name is case-sensitive. Note that the datastore name and the struct field name may be different, tags can be used to change it, e.g.

    AccountEmail string `datastore:"email"`

  • The property must be indexed. Note that whether a property is indexed may vary from entity to entity, so you may have an entity where AccountEmail is indexed and another one where AccountEmail is not indexed.

  • AccountEmail must have a type string. I assume this is trivial and is so. But note that it is possible to save a property with the User type which is different from the string type and when you list entities in the Datastore viewer for example, the email will be displayed just like if it would be an email string, but obviously it is different.

  • To find the user with AccountEmail="email@address.com", the value saved must be "email@address.com" exactly. Lower and upper case letters are different! Spaces (and all whitespace characters) matter! Please check if the saved value is exactly this as you will not see trailing spaces when printed for example, but they will cause a mismatch! Also some unicode characters have the same visual appearance (they look the same) but their unicode codepoint is not the same and will also cause a mismatch.

查看更多
登录 后发表回答