I’ve researched for a while but I can’t seem to get this to work properly and it’s been a frustrating experience, as everything I do keeps getting me this error.
I have the following schema:
@derive {Jason.Encoder, except: [:__meta__, :__struct__, :timer, :inserted_at, :updated_at]}
schema "items" do
field :person_id, :integer
field :status, :integer
field :text, :string
has_many :timer, Timer
many_to_many(:tags, Tag, join_through: ItemTag, on_replace: :delete)
timestamps()
end
I’m using the @derive
annotation to properly decode so I can return the item to the user.
I have this simple function that preloads the tags
associated to an item
accordingly.
def get_item(id, preload_tags \\ false ) do
item = Item
|> Repo.get(id)
if(preload_tags == true) do
item |> Repo.preload(tags: from(t in Tag, order_by: t.text))
else
item
end
end
The problem is that, in the API controller…
# `item` is the object returned from `get_item`. Assume an `item` is found.
item ->
if retrieve_tags do
json(conn, item)
else
item = Map.drop(item, [:tags, :timer])
json(conn, item)
end
if I try to return an item
without tags (else
statement), I keep getting the same error.
no function clause matching in Jason.Encoder.App.Item."-inlined-encode/2-"/2
If I try to to not drop the :tags
property, I get the oh-so familiar error of Jason.Encoder
not being able to encode the association.
** (RuntimeError) cannot encode association :tags from App.Item to JSON because the association was not loaded.
You can either preload the association:
Repo.preload(App.Item, :tags)
Or choose to not encode the association when converting the struct to JSON by explicitly listing the JSON fields in your schema:
defmodule App.Item do
# ...
@derive {Jason.Encoder, only: [:name, :title, ...]}
I want to be able to just return the item
without tags
if the user doesn’t want to. Returning with tags works fine, it’s just that the without tags always errors out with the aforementioned error.
Is my @derive
annotation wrong? It’s the only way I found to serialize tags
property either if it exists or doesn’t.
Thank you very much and I hope you have a wonderful day!