gpt4 book ai didi

elixir - 函数 Poison.encode_to_iodata!/1 未定义

转载 作者:行者123 更新时间:2023-12-02 19:49:10 25 4
gpt4 key购买 nike

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

function Poison.encode_to_iodata!/1 is undefined (module Poison is not available)

但是在我的配置文件中,我添加了 Poison 并使用 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

混合依赖函数

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

最佳答案

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

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

config :phoenix, :format_encoders,
"json-api": Poison

您告诉 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

您应该看看Jason ,Phoenix 现在默认附带它,而且速度“极快”。如果您确实想添加它,则应该将 {:jason, "~> 1.1"} 添加到您的依赖项中,而不是添加 Poison,并替换 "json-api": Poison for "json-api": Jason 在您的 config.exs

关于elixir - 函数 Poison.encode_to_iodata!/1 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58591792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com