rahil627

rahil627

Generated code raises KeyError in core_components.ex for: 'item' and 'col'

greetings. first post/question. sorry to even bother anyone.

i started a project with a lot of generated code, but i’m still having trouble hooking things up (router, context, view), all-the-while learning the framework 'n language. I feel like i’m getting closer to the learning curve’s edge and finding the promised productive-land, so i don’t want to give up yet, but something here just doesn’t seem right to me..:

i’ve got two similar errors that error on core_components.ex. the browser displays these errors:

KeyError at GET /games/1
key :item not found in: %{
  __changed__: nil,
  inner_block: [
    %{
      __slot__: :inner_block,
      inner_block: #Function<8.124877290/2 in GameArchiveWeb.GameHTML.show/1>
    }
  ],
  __given__: %{
    __changed__: nil,
    inner_block: [
      %{
        __slot__: :inner_block,
        inner_block: #Function<8.124877290/2 in GameArchiveWeb.GameHTML.show/1>
      }
    ]
  }
}

and

KeyError at GET /games
key :col not found in: %{
  id: "games",
  rows: [
    %GameArchive.Game{
      __meta__: #Ecto.Schema.Metadata<:loaded, "games">,
      id: 1,
      title: "test",
      alt_titles: ["string 1", "string 2"],
      makers: ["string 1", "string 2"],
      ...
      (LIST ALL Game ROWS FROM DB)
      ...
action: [
      %{
        __slot__: :action,
        inner_block: #Function<30.124877290/2 in GameArchiveWeb.GameHTML.index/1>
      },
      %{
        __slot__: :action,
        inner_block: #Function<31.124877290/2 in GameArchiveWeb.GameHTML.index/1>
      }
    ],
    __changed__: nil,
    inner_block: [
      %{
        __slot__: :inner_block,
        inner_block: #Function<29.124877290/2 in GameArchiveWeb.GameHTML.index/1>
      }
    ],
    row_click: #Function<28.124877290/1 in GameArchiveWeb.GameHTML.index/1>
  },
  __changed__: nil,
  inner_block: [
    %{
      __slot__: :inner_block,
      inner_block: #Function<29.124877290/2 in GameArchiveWeb.GameHTML.index/1>
    }
  ],
  row_click: #Function<28.124877290/1 in GameArchiveWeb.GameHTML.index/1>,
  row_id: nil,
  row_item: &Function.identity/1
}

in core_components.ex:

<div :for={item <- @item} class="flex gap-4 py-4 text-sm leading-6 sm:gap-8">
# and
<th :for={col <- @col} class="p-0 pb-4 pr-6 font-normal"><%= col[:label] %></th>

it seems to me the core_components html template is using/expecting my database schema to have a field named col and item, but that can’t be right.., can it??

my Games.ex (confusing name, i kno) context

  def list_games do
    Repo.all(Game) # i think this selects all by default..
  end

  def get_game!(id) do
    Repo.get(Game, id)
  end

router.ex

    resources "/games", GameController # macro magicks

phew… i hope that’s everything to understand the problem.. there’s just so much abstraction..!

i used a lot of generation stuff: schema first, then html (–no-schema), but the gen’d context file was unimplemented (raise TODO), so i tried to implement it (with the help of docs/context), and now i’m just going through a bunch of errors to get things to work..

again, sorry to bother. I’m thinking maybe it would have been a smoother experience to simply use phx.gen.html to gen everything (schema, context, html, router?) at the same time..

Marked As Solved

garrison

garrison

The KeyErrors are telling you that :item and :col are not found in the assigns map passed to the function components (in this case the table/1 and list/1 components in core_components.ex). You can tell it’s an assigns map because it has keys like __changed__ which are used by LiveView to track changes. Assigns are used like @item in your HEEx templates but they are converted into map operations by the compiler.

In this case, the assigns in question are actually slots which are passed in as assigns by LiveView. The problem is that the @item and @col assigns haven’t been set (you can see in the error message that they are missing from the assigns map), which will happen if you haven’t included any <:col></:col> or <:item></:item> slots in your tables and lists. These are required slots, so they don’t get a default value - you have to provide them.

Missing required slots also emit a compile-time warning telling you that they’re missing - if you check your console output after compilation (or you have inline warnings in your editor) then you should see them.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New

We're in Beta

About us Mission Statement