elixir - returning a list gives (Poison.EncodeError) unable to encode value -
io.puts(inspect( contacts )) gives:
[%hellotable.contact{__meta__: #ecto.schema.metadata<:loaded>, id: 37, inserted_at: #ecto.datetime<2015-10-22t12:50:43z>, name: "gumbo", phone: "(801) 555-55555", updated_at: #ecto.datetime<2015-10-22t12:50:43z>}]
and view looks like:
defmodule hellotable.contactview use hellotable.web, :view def render("index.json", %{contacts: contacts}) io.puts(inspect( contacts )) contacts end end
as try render view get:
** (poison.encodeerror) unable encode value: {nil, "contacts"}
you need either implement poison.encoder
protocol hellotable.contact
described in encoding ecto model json in elixir or return map render function using render_many/4:
defmodule hellotable.contactview use hellotable.web, :view def render("index.json", %{contacts: contacts}) render_many(contacts, __module__, "contact.json") end def render("contact.json", %{contact: contact}) %{ id: contact.id, name: contact.name, phone_number: contact.phone } end end
the above how json handled in phoenix json generators.
Comments
Post a Comment