无法从 Ecto DB 返回 Repo.All in elixir/phoenix



我正在尝试返回我在凤凰城 Ecto 数据库中的所有评论,但我收到一个我不明白的错误。

这是我正在做的事情:

在我的路由中,我调用此函数getComments

def getComments(conn, _params) do
IO.puts("inside getComments")
comments = Repo.all(Comments)
IO.puts(inspect(comments))
# json conn, comments
render(conn, "comments.json", comments)
end

它调用具有以下方法的视图文件:

def render("comments.json", %{comments: comments}) do
IO.puts("inside comments.json and value of comments")
%{data: render_many(comments, PageView, "onecomment.json")}
end
def render("onecomment.json", %{comment: comment}) do
IO.puts("inside onecomment.json")
IO.puts(inspect(comment))
%{
children: comment.children,
downvotes: comment.downvotes,
upvotes: comment.upvotes,
message: comment.message,
parent: comment.parent,
}
end    

本教程(https://becoming-functional.com/building-a-rest-api-with-phoenix-1-3-part-1-9f8754aeaa87(似乎表明这是正确的方法。我也(成功(打印出评论的内容,我有一个条目。您可以在此处看到带有错误的终端输出:

[info] GET /getComments
inside getComments
[debug] Processing with AlbatrossWeb.PageController.getComments/2
Parameters: %{}
Pipelines: [:browser]
[debug] QUERY OK source="comment" db=2.4ms decode=3.4ms
SELECT c0."id", c0."children", c0."downvotes", c0."message", c0."parent", c0."upvotes", c0."inserted_at", c0."updated_at" FROM "comment" AS c0 []
[%Albatross.Comments{__meta__: #Ecto.Schema.Metadata<:loaded, "comment">, children: nil, downvotes: 0, id: 1, inserted_at: ~N[2018-10-05 20:32:42.930021], message: "sdf", parent: nil, updated_at: ~N[2018-10-05 20:32:42.931840], upvotes: 0}]
[info] Sent 500 in 63ms
[error] #PID<0.401.0> running AlbatrossWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: GET /getComments
** (exit) an exception was raised:
** (ArgumentError) argument error
(stdlib) :maps.from_list([%Albatross.Comments{__meta__: #Ecto.Schema.Metadata<:loaded, "comment">, children: nil, downvotes: 0, id: 1, inserted_at: ~N[2018-10-05 20:32:42.930021], message: "sdf", parent: nil, updated_at: ~N[2018-10-05 20:32:42.931840], upvotes: 0}])
(phoenix) lib/phoenix/controller.ex:770: Phoenix.Controller.to_map/1
(phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.do_render/4
(albatross) lib/albatross_web/controllers/page_controller.ex:1: AlbatrossWeb.PageController.action/2
(albatross) lib/albatross_web/controllers/page_controller.ex:1: AlbatrossWeb.PageController.phoenix_controller_pipeline/2
(albatross) lib/albatross_web/endpoint.ex:1: AlbatrossWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
(albatross) lib/albatross_web/endpoint.ex:1: AlbatrossWeb.Endpoint.plug_builder_call/2
(albatross) lib/plug/debugger.ex:122: AlbatrossWeb.Endpoint."call (overridable 3)"/2
(albatross) lib/albatross_web/endpoint.ex:1: AlbatrossWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/patientplatypus/Documents/zennifyblog/backend/albatross/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

我不明白这个错误。maps.from_list出了点问题,所以我猜它不喜欢我以某种方式映射值的方式?但同样,将其与我上面链接的教程中的视图示例进行比较:

defmodule Api13Web.UserView do
use Api13Web, :view
alias Api13Web.UserView
def render("index.json", %{users: users}) do
%{data: render_many(users, UserView, "user.json")}
end
def render("show.json", %{user: user}) do
%{data: render_one(user, UserView, "user.json")}
end
def render("user.json", %{user: user}) do
%{id: user.id,
email: user.email,
password: user.password,
age: user.age,
stooge: user.stooge}
end
end

这似乎几乎和我所做的一模一样不是吗?

你需要传递关键字列表来渲染函数

render(conn, "comments.json", comments: comments )

我更喜欢使用如下所示的管道,当分配太多时看起来更好

conn |> assign(:comments, comments) |> render("comments.json")

最新更新