When attempting to render json data I get the following error in my controller.
Poison.EncodeError at GET /api
unable to encode value: {nil, "paths"}
web/controllers/api_controller.ex:1 App.ApiController.action/2
After searching around I attempted to my model like this:
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
Which doesn't seem to resolve the error. I got this error after attempting to preload my paths field in my controller like this:
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
I'm able to insert data find into my database and I can query it all with:
Repo.all(Api) |> Repo.preload(:paths)
Any thoughts on what else to try? Thanks
If you want to preload your :paths
, you should use derive
on Documentr2.Path
module too.
@derive [Poison.Encoder]
or
@derive {Poison.Encoder, only: [:field_you_want]}