【发布时间】:2017-01-23 15:06:42
【问题描述】:
客户端发送以下 JSON:{"user": {"age+":18,"age-":40}}
在我的控制器中,我有以下内容:
def procura(conn, query) do
maior = conn.params["user"]["age+"]
menor = conn.params["user"]["age-"]
query = from u in query, where: u.age > ^maior and u.age < ^menor, select: u.name
pesquisa = Repo.all query
IO.puts pesquisa
end
但我收到了编译器警告:** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for %{}
如何为 Ecto 查询传递 JSON 中的值?
我的目标是基于 JSON 字段进行查询。我想查询年龄在 x 到 y 之间的用户。 自动翻译。
【问题讨论】:
-
查询的价值是什么?您似乎将
%{}作为第二个参数传递给procura而不是查询。此外,您需要将from行更改为query = from u in query, ...。 -
procura 是动作吗?如果是,则查询将从请求中清除参数。请在 show 上尝试查询 IO.inspect 作为结果。应该是地图。如果它是一个动作,你应该返回一个连接。
-
您好!我的目标是基于 JSON 字段进行搜索。我想搜索年龄在 x 到 y 之间的用户。我没有得到。
标签: json elixir phoenix-framework ecto