代码之家  ›  专栏  ›  技术社区  ›  Peter Weyand

无法返回回购。全部来自elixir/phoenix中的Exto DB

  •  0
  • Peter Weyand  · 技术社区  · 6 年前

    我试图返回所有的评论,我在凤凰城的星外数据库,我得到了一个错误,我不明白。

    在我的路线中,我称之为这个函数 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 回复  |  直到 6 年前
        1
  •  1
  •   quazar    6 年前

    您需要传递关键字列表来呈现函数

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

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