函数 Poison.encode_to_iodata!/1 未定义

Sid*_*ngh 2 elixir phoenix-framework

我正在开发一个 elixir API,我正在使用 JaSerializer 但它在我调用 post API 时给了我一个类似这样的错误。

function Poison.encode_to_iodata!/1 is undefined (module Poison is not available)
Run Code Online (Sandbox Code Playgroud)

但是在我的配置文件中,我添加了 Poison 并使用 mime 重新编译

mix deps.clean mime --build
mix deps.get 
Run Code Online (Sandbox Code Playgroud)

我正在调用的 api 也被插入到数据库中,但它没有响应任何 Json 并给我那个错误。

这是我的配置文件:

use Mix.Config

config :banking,
  ecto_repos: [Banking.Repo]

# Configures the endpoint
config :banking, BankingWeb.Endpoint,
  url: [host: "localhost"],
  secret_key_base: "LMcWbj2QramA5ulI0MZnFobOtrKg/Z2x/gmDl6NwH7hxUbRksPzZjuXwyk8QKGyx",
  render_errors: [view: BankingWeb.ErrorView, accepts: ~w(json)],
  pubsub: [name: Banking.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

config :mime, :types, %{
  "application/vnd.api+json" => ["json-api"]
}

config :phoenix, :format_encoders,
  "json-api": Poison
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误

[error] #PID<0.583.0> running BankingWeb.Endpoint (connection #PID<0.582.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /api/banks
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function Poison.encode_to_iodata!/1 is undefined (module Poison is not available)
        Poison.encode_to_iodata!(%{"data" => %{"attributes" => %{"name" => "arvind singh"}, "id" => "19", "type" => "bank"}, "jsonapi" => %{"version" => "1.0"}})
        (phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.__put_render__/5
        (phoenix) lib/phoenix/controller.ex:746: Phoenix.Controller.instrument_render_and_send/4
        (banking) lib/banking_web/controllers/bank_controller.ex:1: BankingWeb.BankController.action/2
        (banking) lib/banking_web/controllers/bank_controller.ex:1: BankingWeb.BankController.phoenix_controller_pipeline/2
        (phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
        (banking) lib/banking_web/endpoint.ex:1: BankingWeb.Endpoint.plug_builder_call/2
        (banking) lib/plug/debugger.ex:122: BankingWeb.Endpoint."call (overridable 3)"/2
        (banking) lib/banking_web/endpoint.ex:1: BankingWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy) /Users/apple/Documents/bankingassignment/banking/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /Users/apple/Documents/bankingassignment/banking/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
        (cowboy) /Users/apple/Documents/bankingassignment/banking/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Run Code Online (Sandbox Code Playgroud)

Mix deps 函数

defp deps do
    [
      {:phoenix, "~> 1.4.9"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.1"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:ja_serializer, github: "vt-elixir/ja_serializer"}
    ]
  end
Run Code Online (Sandbox Code Playgroud)

sba*_*rob 5

问题是您忘记包含Poison在您的依赖项中(depsmix.exs文件中的函数)。

当您将以下内容放入配置中时:

config :phoenix, :format_encoders,
  "json-api": Poison
Run Code Online (Sandbox Code Playgroud)

你告诉 Phoenix 它应该使用 Poison 来处理 JSON。因此,您应该Poison在您的应用程序中使用。所以,你应该把它添加到你的deps函数中(它在最后):

  defp deps do
    [
      {:phoenix, "~> 1.4.9"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.1"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:ja_serializer, github: "vt-elixir/ja_serializer"},
      {:poison, "~> 3.1"}
    ]
  end
Run Code Online (Sandbox Code Playgroud)

你应该看看Jason,Phoenix 现在默认附带它,它“非常快”。如果你想补充一点,你应该添加{:jason, "~> 1.1"}到您的依赖,而不是加入毒药,并替换"json-api": Poison"json-api": Jason您的config.exs