Elixir 函数毒物。将_编码为_iodata/1是未定义的

Elixir 函数毒物。将_编码为_iodata/1是未定义的,elixir,phoenix-framework,Elixir,Phoenix Framework,我正在开发一个ElixirAPI,我正在使用JaSerializer,但是在调用PostAPI时,它给了我一个类似这样的错误 function Poison.encode_to_iodata!/1 is undefined (module Poison is not available) 但在我的配置文件中,我添加了毒药并使用mime重新编译 mix deps.clean mime --build mix deps.get 我正在调用的api也被插入到数据库中,但它没有响应任何Json,并

我正在开发一个ElixirAPI,我正在使用JaSerializer,但是在调用PostAPI时,它给了我一个类似这样的错误

function Poison.encode_to_iodata!/1 is undefined (module Poison is not available)
但在我的配置文件中,我添加了毒药并使用mime重新编译

mix deps.clean mime --build
mix deps.get 
我正在调用的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"
这就是我得到的错误

[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

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

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

config :phoenix, :format_encoders,
  "json-api": Poison
你告诉Phoenix它应该使用毒药来处理JSON。因此,应用程序中应该有
毒药。因此,您应该将其添加到
deps
函数中(位于末尾):


你应该看一看,凤凰城现在默认附带了它,而且它“燃烧得很快”。如果您确实想添加它,您应该将
{:jason,“~>1.1”}
添加到您的依赖项中,而不是添加毒药,并将
“json-api”:毒药替换为
“json-api”:jason
config.exs
中的
(您的
deps
函数位于
mix.exs
文件中)

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

config :phoenix, :format_encoders,
  "json-api": Poison
您告诉Phoenix它应该使用毒药来处理JSON。因此,您的应用程序中应该有
Poison
。因此,您应该将其添加到
deps
函数中(位于末尾):


您应该看看,Phoenix现在默认附带了它,而且它“燃烧得很快”。如果您确实想添加它,您应该将
{:jason,“~>1.1”}
添加到您的依赖项中,而不是添加毒药,并将
“json api”:毒药替换为
“json api”:Jason
在您的
config.exs

中,您的deps中定义了哪个版本?您可以尝试运行
mix-deps.update--all
?您还可以添加您的mix.exs文件(其deps/0函数)吗?是的,我这样做了。仍然会收到相同的错误!@julp您现在可以看一下吗?您需要添加
{:poison,“~>3.1”}
到您的deps,然后再次运行
mix-deps.get
在您的deps中定义了哪个版本?您可以尝试运行
mix-deps.update--all
?您还可以添加您的mix.exs文件(它的deps/0函数)吗?是的,我这样做了。仍然会遇到相同的错误!@julp您现在可以看一下吗?您需要添加
{:poison,~>3.1}
到您的deps,然后运行
mix deps.get
againI以前使用过Jason,但我如何比较这两种方法对我的代码更好?我个人认为使用“json api”:Poison节省了大量时间并提供了更多详细信息您可以运行基准测试。为此,您可以查看。您可以使用benchee编码/解码简单对象,在本地运行两个测试,或者测试应用程序中实际发生的事情,这样您就可以很好地了解它们的比较情况。您到底要做什么你的意思是毒药会提供更多细节?我以前使用过Jason,但我如何比较这两者对我的代码更有利?我个人认为使用“json api”:Poison节省了大量时间并提供了更多详细信息您可以运行基准测试。为此,您可以查看。您可以使用benchee编码/解码简单对象,在本地运行两个测试,或者测试应用程序中实际发生的事情,这样您就可以很好地了解它们的比较情况。您到底要做什么你是说毒药能提供更多细节?
  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