thijsvtol
I’m currently using a model button that referended is to another list of objects called items
When I make a POST call to the service it returns the following error message:
[error] #PID<0.440.0> running BuyButtonServiceWeb.Endpoint (connection #PID<0.430.0>, stream id 3) terminated
Server: localhost:4000 (http)
Request: POST /button
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %BuyButtonService.Dashboard.Item{__meta__: #Ecto.Schema.Metadata<:loaded, "items">, button: #Ecto.Association.NotLoaded<association :button is not loaded>, buttonId: 1, button_id: nil, id: 1, ingredient: "string", inserted_at: ~U[2021-01-20 14:52:19Z], productId: 0, quantity: 0, updated_at: ~U[2021-01-20 14:52:19Z]} of type BuyButtonService.Dashboard.Item (a struct), Jason.Encoder protocol must always be explicitly implemented.
If you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:
@derive {Jason.Encoder, only: [....]}
defstruct ...
My controller:
def create(conn, %{"button" => button_params}) do
with {:ok, %Button{} = button} <- Dashboard.create_button(button_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.button_path(conn, :show, button))
|> render("show.json", button: button)
end
end
Button model:
defmodule BuyButtonService.Dashboard.Button do
use Ecto.Schema
import Ecto.Changeset
alias BuyButtonService.Dashboard.Item, as: Item
@derive Jason.Encoder
schema "button" do
has_many :items, {"items", Item}, foreign_key: :buttonId
field :name, :string
field :userId, :integer
timestamps([type: :utc_datetime])
end
@doc false
def changeset(button, attrs) do
button
|> cast(attrs, [:userId, :name])
|> cast_assoc(:items, with: &Item.changeset/2)
|> validate_required([:userId, :name, :items])
end
end
Items model:
defmodule BuyButtonService.Dashboard.Item do
use Ecto.Schema
import Ecto.Changeset
alias BuyButtonService.Dashboard.Button, as: Button
schema "items" do
field :buttonId, :integer
belongs_to :button, Button
field :ingredient, :string
field :productId, :integer
field :quantity, :integer
timestamps([type: :utc_datetime])
end
@doc false
def changeset(item, attrs) do
item
|> cast(attrs, [:buttonId, :ingredient, :productId, :quantity])
|> validate_required([:ingredient])
end
end
When using the @derive {Jason.Encoder, only: [....]} it says that there can only be 1 destruct
Can someone tell me how to handle this or have another solution?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Can you post the code that triggers that error?
schemausesdefstructas part of its implementation, so you’ll get a similar-sounding error if you write:Side note:
This is likely not what you want - unless your table has both a
buttonIdandbutton_idcolumnbelongs_towill callfieldwith the value it’s given forforeign_key(or by adding_idon the end of the name if that’s not supplied)thijsvtol
The issue was in
button_view.exit was trying to load:itemsbut could not. Now this one is solved by adding a render_many to the button render.Now create and delete works. If I’m trying to get a single button e.g.
localhost:4000/button/1it’s returning the following error:Could this be the problem with the
belongs_torelation? May you explain how to relate button table into items table.Button migration:
Items migration:
John-Goff
If you read the stack trace, you can see that the first error it shows is this
What this error is telling you is that you have fetched a button that has_many items, but you have not fetched the items. The
Ecto.Association.NotLoadedstruct is the default value if you have not preloaded your associations. To fix this you need to add a preload to your query or somewhere else before your view.sergio
For my use case, I had a function that returned tuples, and I needed the tuples to render a chart.js chart.
So here’s what I did:
In my liveview:
Now in Javascript I can access it: