Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly run a nested filter? #394

Closed
munjalpatel opened this issue Dec 11, 2016 · 2 comments
Closed

How to properly run a nested filter? #394

munjalpatel opened this issue Dec 11, 2016 · 2 comments

Comments

@munjalpatel
Copy link

munjalpatel commented Dec 11, 2016

I am trying to run a filter on a property of a nested object for RethinkDB in Golang. But I am sure that I am missing something here. Could you please help me out?

This is the error I am getting:

(func literal).Eq undefined (type func(gorethink.Term) gorethink.Term has no field or method Eq)

Here is my code:

type User struct {
	Id        string `json:"id,omitempty"`
	FirstName string `json:"firstName,omitempty"`
	LastName  string `json:"lastName,omitempty"`
	Email     string `json:"email,omitempty"`
	Password  string `json:"password,omitempty"`
	Salt      string `json:"salt,omitempty"`
}

type UnverifiedUserRequest struct {
	Id    string `json:"id,omitempty"`
	Token string `json:"token,omitempty"`
	User  User   `json:"user,omitempty"`
}

db.Table("unverified_requests").Filter(func(row r.Term) r.Term {
return row.Field("user").Map(func(user r.Term) r.Term {
        return user.Field("email")
    }.Eq(email))
}).Run(session)
@thomasmodeneis
Copy link

You are missing the closing ) for the .Map function.
Something like this should at least compile, but it will fail with: Cannot convert STRING to SEQUENCE.

r.Table("unverified_requests").Filter(func(row r.Term) r.Term {
			return row.Field("user").Map(func(user r.Term) r.Term {
				return user.Field("email")
			}).Eq("test")
		}).Run(session)

I've never seen such composition, map can only be applied to sequences, not single values.

This query will be better written if you create a index for the field email and then use it with GetAllByIndex:

r.Table("unverified_requests").GetAllByIndex("email", "john@mail.com").Run(session)

Cheers.

@thewraven
Copy link

I think that the best approach to your query would be using filter with a function argument . The documentation of the javascript can be very helpful, you can check it out here .

I've created a gist for your specific question, I hope it works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants