Poison.EncodeError 在 GET /api 无法编码值:{nil,"paths"}
Poison.EncodeError at GET /api unable to encode value: {nil, "paths"}
尝试呈现 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
我可以将查找到的数据插入到我的数据库中,并且可以通过以下方式查询:
Repo.all(Api) |> Repo.preload(:paths)
还有什么想尝试的吗?谢谢
如果您想预加载 :paths
,您也应该在 Documentr2.Path
模块上使用 derive
。
@derive [Poison.Encoder]
或
@derive {Poison.Encoder, only: [:field_you_want]}
尝试呈现 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
我可以将查找到的数据插入到我的数据库中,并且可以通过以下方式查询:
Repo.all(Api) |> Repo.preload(:paths)
还有什么想尝试的吗?谢谢
如果您想预加载 :paths
,您也应该在 Documentr2.Path
模块上使用 derive
。
@derive [Poison.Encoder]
或
@derive {Poison.Encoder, only: [:field_you_want]}