【问题标题】:Poison.EncodeError at GET /api unable to encode value: {nil, "paths"}Poison.EncodeError at GET /api 无法编码值:{nil, "paths"}
【发布时间】:2018-02-01 19:18:34
【问题描述】:

尝试渲染 json 数据时,我的控制器中出现以下错误。

Poison.EncodeError at GET /api
unable to encode value: {nil, "paths"}

web/controllers/api_controller.ex:1 App.ApiController.action/2

四处搜索后,我尝试了这样的模型:

defmodule App.Api do
  use App.Web, :model

  @derive {Poison.Encoder, only: [:basePath, :definitions, :paths]}
  schema "apis" do
    field :basePath, :string
    field :definitions, :string
    has_many :paths, App.Path

    timestamps()
  end
end

这似乎无法解决错误。尝试像这样在控制器中预加载路径字段后出现此错误:

defmodule App.ApiController do
  use App.Web, :controller

  alias App.Api

  def index(conn, _params) do
    apis = Repo.all(Api) |> Repo.preload(:paths)
    render conn, "index.json", apis: apis
  end
end

我可以将数据 find 插入到我的数据库中,并且可以通过以下方式查询所有内容:

Repo.all(Api) |> Repo.preload(:paths)

还有什么可以尝试的想法吗?谢谢

【问题讨论】:

    标签: elixir phoenix-framework elixir-poison


    【解决方案1】:

    如果你想预加载你的:paths,你也应该在Documentr2.Path模块上使用derive

    @derive [Poison.Encoder]
    

    @derive {Poison.Encoder, only: [:field_you_want]}
    

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      相关资源
      最近更新 更多