简体   繁体   English

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

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

I'm trying to get Phoenix to return JSON:API format. 我正在尝试让Phoenix返回JSON:API格式。

After running the generic generator, the generic json endpoint works. 运行通用生成器后,通用json端点起作用。 When I try to follow the How to use with Phoenix docs for getting it to respond in JSON:API format, I'm getting a no function clause in render/2 error. 当我尝试遵循“ 如何与Phoenix文档一起使用”以使其以JSON:API格式响应时,我在render / 2错误中遇到了no function子句。

Error: 错误:

[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

My files: 我的文件:

View: 视图:

defmodule ApiWeb.UpdateRequestView do
  use JSONAPI.View

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

Controller: 控制器:

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

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

I've never used this library, but according to docs: 我从来没有使用过这个库,但是根据文档:

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

So your calls to render should probably be adjusted to: 因此,您对render的调用可能应该调整为:

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

Or something like that 或类似的东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elixir :( FunctionClauseError)没有函数子句匹配 - Elixir: (FunctionClauseError) no function clause matching Phoenix 1.4升级-(FunctionClauseError)Phoenix.Socket中没有匹配的函数子句。__terminate __ / 2 - Phoenix 1.4 Upgrade - (FunctionClauseError) no function clause matching in Phoenix.Socket.__terminate__/2 Elixir Pheonix通道(FunctionClauseError)ProjectName.ModuleName.handle_in / 3中没有匹配的功能子句 - Elixir Pheonix Channels (FunctionClauseError) no function clause matching in ProjectName.ModuleName.handle_in/3 (FunctionClauseError)Access.get / 3中没有匹配的功能子句 - (FunctionClauseError) no function clause matching in Access.get/3 Elixir Phoenix:控制器测试中出现奇怪的“无功能子句匹配”错误 - Elixir Phoenix: Strange “no function clause matching” error in Controller Test (FunctionClauseError) 没有 function 子句匹配 CheckersgameWeb.GamesChannel.handle_in/3 - (FunctionClauseError) no function clause matching in CheckersgameWeb.GamesChannel.handle_in/3 Elixir-插件-无功能子句匹配 - Elixir - Plug - no function clause matching Elixir / Phoenix 1.3拆分上下文 - Elixir/Phoenix 1.3 splitting context (FunctionClauseError)Access.get / 3中没有匹配的功能子句-单独的问题,因为该主题中的其他问题被删除 - (FunctionClauseError) no function clause matching in Access.get/3 - separate question because additional ones in the topic are being deleted 无法运行任何混合命令-**(FunctionClauseError)IO.chardata_to_string / 1中没有匹配的功能子句 - Can't run any mix command - ** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM