Golang Rethinkdb嵌套过滤器

huangapple go评论77阅读模式
英文:

Golang Rethinkdb nested filter

问题

我正在尝试在Golang中使用RethinkDB对嵌套对象的属性运行过滤器,但我确定我在这里漏掉了一些东西。

这是我得到的错误:

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

这是我的代码:

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)

更新 1

根据 @icza 的建议进行更改后,程序可以成功构建。但在运行时我得到了以下错误:

gorethink: Cannot convert OBJECT to SEQUENCE in: r.DB("uc_dev").Table("unverified_requests").Filter(func(var_5 r.Term) r.Term { return var_5.Field("user").Map(func(var_6 r.Term) r.Term { return var_6.Field("email") }).Eq("myemail@gmail.com") })

英文:

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.

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)

UPDATE 1

After making changes as suggested by @icza, the program builds fine. But I am getting the following error at runtime:

> gorethink: Cannot convert OBJECT to SEQUENCE in: r.DB("uc_dev").Table("unverified_requests").Filter(func(var_‌​5 r.Term) r.Term { return var_5.Field("user").Map(func(var_6 r.Term) r.Term { return var_6.Field("email") }).Eq("myemail@gmail.com") })

答案1

得分: 1

一个括号放错了位置。应该使用}).Eq(email)而不是}.Eq(email))

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)

你试图在传递给Map()的函数字面量参数上调用Eq()。显然,它不是gorethink.Term类型,但gorethink.Map()的返回值是,所以在Map()的括号关闭后调用Eq(),也就是将.Eq()放在Map()方法调用的括号之后。

英文:

A misplaced parenthesis. Instead of }.Eq(email)) use }).Eq(email):

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)

You tried to call Eq() on the function literal argument that is passed to Map(). It is obviously not of type gorethink.Term, but the return value of gorethink.Map() is, so call Eq() on the reutrn value of Map(), which means put the .Eq() after the parenthesis that closes the method call Map().

huangapple
  • 本文由 发表于 2016年12月12日 00:22:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/41088294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定