Elixir GET/api处的Poison.EncodeError无法对值进行编码:{nil,“paths”}

Elixir GET/api处的Poison.EncodeError无法对值进行编码:{nil,“paths”},elixir,phoenix-framework,elixir-poison,Elixir,Phoenix Framework,Elixir Poison,当试图呈现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.Encod

当试图呈现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
这似乎无法解决错误。我在尝试在控制器中预加载路径字段后出现此错误,如下所示:

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
我可以将查找到的数据插入我的数据库,并可以使用以下工具进行查询:

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

你有没有其他的想法?谢谢

如果您想预加载:路径,也应该使用Documentr2.Path模块上的派生

@derive [Poison.Encoder]