Phoenix 1.3 Elixir jsonapi(FunctionClauseError)在render / 2中没有匹配的函数子句

时间:2019-01-21 16:53:39

标签: elixir phoenix-framework json-api

我正试图让Phoenix返回JSON:API格式。

运行通用生成器后,通用json端点起作用。当我尝试遵循How to use with Phoenix文档以使其以JSON:API格式响应时,我在render / 2错误中遇到了no function子句。

错误:

[info] Running ApiWeb.Endpoint with cowboy 2.6.1 at http://localhost:4000
[info] GET /api/update_requests
[debug] Processing with ApiWeb.UpdateRequestController.index/2
  Parameters: %{}
  Pipelines: [:api]
[debug] QUERY OK source="update_requests" db=30.2ms decode=2.4ms queue=3.8ms
SELECT u0."id", u0."email", u0."name", u0."inserted_at", u0."updated_at" FROM "update_requests" AS u0 []
[info] Sent 500 in 165ms
[error] #PID<0.524.0> running ApiWeb.Endpoint (connection #PID<0.523.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/update_requests

我的文件:

查看:

defmodule ApiWeb.UpdateRequestView do
  use JSONAPI.View

  def fields do
    [:name, :email, :id]
  end
end

控制器:

defmodule ApiWeb.UpdateRequestController do
  use ApiWeb, :controller

  alias Api.Marketing
  alias Api.Marketing.UpdateRequest

  action_fallback ApiWeb.FallbackController

  def index(conn, _params) do
    update_requests = Marketing.list_update_requests()
    render(conn, "index.json", update_requests: update_requests)
  end

  def create(conn, %{"update_request" => update_request_params}) do
    with {:ok, %UpdateRequest{} = update_request} <-
           Marketing.create_update_request(update_request_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", Routes.update_request_path(conn, :show, update_request))
      |> render("show.json", update_request: update_request)
    end
  end

  def show(conn, %{"id" => id}) do
    update_request = Marketing.get_update_request!(id)
    render(conn, "show.json", update_request: update_request)
  end
end

mix.exs:

  defp deps do
    [
      {:phoenix, "~> 1.4.0"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.0"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:jsonapi, "~> 0.9.0"}
    ]
  end

config.ex:

...
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :jsonapi,
  field_transformation: :underscore,
  remove_links: true,
  json_library: Jason

# Import environment specific config. This must remain at the bottom
...

差异:

diff --git a/config/config.exs b/config/config.exs

--- a/config/config.exs
+++ b/config/config.exs
@@ -25,6 +25,11 @@ config :logger, :console,
 # Use Jason for JSON parsing in Phoenix
 config :phoenix, :json_library, Jason

+config :jsonapi,
+  field_transformation: :underscore,
+  remove_links: true,
+  json_library: Jason
+

diff --git a/lib/api_web/views/update_request_view.ex b/lib/api_web/views/update_request_view.ex
 defmodule ApiWeb.UpdateRequestView do
-  use ApiWeb, :view
-  alias ApiWeb.UpdateRequestView
+  use JSONAPI.View

-  def render("index.json", %{update_requests: update_requests}) do
-    %{data: render_many(update_requests, UpdateRequestView, "update_request.json")}
-  end
-
-  def render("show.json", %{update_request: update_request}) do
-    %{data: render_one(update_request, UpdateRequestView, "update_request.json")}
-  end
-
-  def render("update_request.json", %{update_request: update_request}) do
-    %{id: update_request.id, name: update_request.name, email: update_request.email}
+  def fields do
+    [:name, :email, :id]
   end
 end
diff --git a/mix.exs b/mix.exs
@@ -40,7 +40,8 @@ defmodule Api.MixProject do
       {:postgrex, ">= 0.0.0"},
       {:gettext, "~> 0.11"},
       {:jason, "~> 1.0"},
-      {:plug_cowboy, "~> 2.0"}
+      {:plug_cowboy, "~> 2.0"},
+      {:jsonapi, "~> 0.9.0"}
     ]
   end

diff --git a/mix.lock b/mix.lock
...

1 个答案:

答案 0 :(得分:0)

我从来没有使用过这个库,但是根据文档:

  

您现在可以正常调用render(conn,MyApp.PostView,“ show.json”,%{data:my_data,meta:meta})或'index.json。

因此,您对渲染的调用可能应该调整为:

render(conn, ApiWeb.UpdateRequestView, "show.json", %{data: update_request})

或者类似的东西