thijsvtol

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?

Showing Posts 1 to 4

al2o3cr

al2o3cr

Can you post the code that triggers that error? schema uses defstruct as part of its implementation, so you’ll get a similar-sounding error if you write:

defstruct [:some_fields]
schema "items" do # <---- will try to defstruct as well

Side note:

  field :buttonId, :integer
  belongs_to :button, Button

This is likely not what you want - unless your table has both a buttonId and button_id column :thinking:

belongs_to will call field with the value it’s given for foreign_key (or by adding _id on the end of the name if that’s not supplied)

thijsvtol

thijsvtol OP

The issue was in button_view.ex it was trying to load :items but 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/1 it’s returning the following error:

[debug] Processing with BuyButtonServiceWeb.ButtonController.show/2
  Parameters: %{"id" => "13"}
  Pipelines: [:api]
[debug] QUERY OK source="button" db=0.0ms idle=188.0ms
SELECT b0."id", b0."name", b0."userId", b0."inserted_at", b0."updated_at" FROM "button" AS b0 WHERE (b0."id" = $1) [13]
[error] #PID<0.473.0> running BuyButtonServiceWeb.Endpoint (connection #PID<0.472.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /button/13
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ecto.Association.NotLoaded<association :items is not loaded> of type Ecto.Association.NotLoaded (a struct)
        (elixir 1.11.2) lib/enum.ex:1: Enumerable.impl_for!/1
        (elixir 1.11.2) lib/enum.ex:141: Enumerable.reduce/3
        (elixir 1.11.2) lib/enum.ex:3461: Enum.map/2
        (buy_button_service 0.1.0) lib/buy_button_service_web/views/button_view.ex:19: BuyButtonServiceWeb.ButtonView.render/2
        (buy_button_service 0.1.0) lib/buy_button_service_web/views/button_view.ex:11: BuyButtonServiceWeb.ButtonView.render/2
        (phoenix 1.5.7) lib/phoenix/view.ex:472: Phoenix.View.render_to_iodata/3
        (phoenix 1.5.7) lib/phoenix/controller.ex:776: Phoenix.Controller.render_and_send/4
        (buy_button_service 0.1.0) lib/buy_button_service_web/controllers/button_controller.ex:1: BuyButtonServiceWeb.ButtonController.action/2
        (buy_button_service 0.1.0) lib/buy_button_service_web/controllers/button_controller.ex:1: BuyButtonServiceWeb.ButtonController.phoenix_controller_pipeline/2
        (phoenix 1.5.7) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
        (buy_button_service 0.1.0) lib/buy_button_service_web/endpoint.ex:1: BuyButtonServiceWeb.Endpoint.plug_builder_call/2
        (buy_button_service 0.1.0) lib/plug/debugger.ex:132: BuyButtonServiceWeb.Endpoint."call (overridable 3)"/2
        (buy_button_service 0.1.0) lib/buy_button_service_web/endpoint.ex:1: BuyButtonServiceWeb.Endpoint.call/2
        (phoenix 1.5.7) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.8.0) c:/Users/Van Tol/Documents/GitHub/Elixir/labs-buy-button-v2-service/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.8.0) c:/Users/Van Tol/Documents/GitHub/Elixir/labs-buy-button-v2-service/deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
        (cowboy 2.8.0) c:/Users/Van Tol/Documents/GitHub/Elixir/labs-buy-button-v2-service/deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
        (stdlib 3.8) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Could this be the problem with the belongs_to relation? May you explain how to relate button table into items table.

Button migration:

defmodule BuyButtonService.Repo.Migrations.CreateButton do
  use Ecto.Migration
  alias BuyButtonService.Dashboard.Item, as: Item

  def change do
    create table(:button) do
      add :userId, :integer
      add :name, :string
      add :items, references(:items)

      timestamps([type: :utc_datetime])
    end
  end
end

Items migration:

defmodule BuyButtonService.Repo.Migrations.CreateItem do
  use Ecto.Migration

  def change do
    create table(:items) do
      add :buttonId, :integer
      add :ingredient, :string
      add :productId, :integer
      add :quantity, :integer

      timestamps([type: :utc_datetime])
    end
  end
end

John-Goff

John-Goff

If you read the stack trace, you can see that the first error it shows is this

** (Protocol.UndefinedError) protocol Enumerable not implemented for #Ecto.Association.NotLoaded<association :items is not loaded> of type Ecto.Association.NotLoaded (a struct) 

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.NotLoaded struct 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

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:

<canvas data-points={Jason.encode!(@daily_views)} id="laka-samboody" phx-hook="BarChart">
</canvas>

In my liveview:

def assign_daily_views(socket) do
  daily_views =
    socket.assigns.item
    |> Items.daily_views_for_past_days(30)
    |> Enum.map(&Tuple.to_list(&1)) # <---- Key fix here buddy!

  assign(socket, :daily_views, daily_views)
end

Now in Javascript I can access it:

export const BarChart = {
  mounted() {
    console.log(this.el.dataset.points);
— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews