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

时间:2018-10-05 21:55:26

标签: json database rest elixir phoenix-framework

我正在尝试返回我在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

这似乎和我没有做的完全一样?

1 个答案:

答案 0 :(得分:1)

您需要传递关键字列表以呈现功能

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

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

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