簡體   English   中英

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

[英]Phoenix 1.3 Elixir jsonapi (FunctionClauseError) no function clause matching in render/2

我正在嘗試讓Phoenix返回JSON:API格式。

運行通用生成器后,通用json端點起作用。 當我嘗試遵循“ 如何與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:

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
...

我從來沒有使用過這個庫,但是根據文檔:

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

因此,您對render的調用可能應該調整為:

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

或類似的東西

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM