smahi

smahi OP

When uploading a file using live_view form the following error is thrown

[error] GenServer #PID<0.832.0> terminating
** (KeyError) key "phx-Fseb32TVk9l_8wKC" not found in: %{"phx-Fseb4rMTYjuATwHG" => :visitors_file}
    :erlang.map_get("phx-Fseb32TVk9l_8wKC", %{"phx-Fseb4rMTYjuATwHG" => :visitors_file})
    (phoenix_live_view 0.17.5) lib/phoenix_live_view/upload.ex:186: Phoenix.LiveView.Upload.get_upload_by_ref!/2
    (phoenix_live_view 0.17.5) lib/phoenix_live_view/channel.ex:1056: anonymous fn/3 in Phoenix.LiveView.Channel.maybe_update_uploads/2
    (stdlib 3.17) maps.erl:410: :maps.fold_1/3
    (phoenix_live_view 0.17.5) lib/phoenix_live_view/channel.ex:490: anonymous fn/5 in Phoenix.LiveView.Channel.component_handle_event/6
    (phoenix_live_view 0.17.5) lib/phoenix_live_view/diff.ex:206: Phoenix.LiveView.Diff.write_component/4
    (phoenix_live_view 0.17.5) lib/phoenix_live_view/channel.ex:488: Phoenix.LiveView.Channel.component_handle_event/6
    (stdlib 3.17) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.17) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{event: "event", join_ref: "15", payload: %{"cid" => 2, "event" => "validate-upload", "type" => "form", "uploads" => %{"phx-Fseb32TVk9l_8wKC" => [%{"name" => "Copie de liste_dossiers_inscriptions_.xls", "path" => "visitors_file", "ref" => "0", "size" => 72192, "type" => "application/vnd.ms-excel"}]}, "value" => "_target=visitors_file"}, ref: "16", topic: "lv:phx-Fseb3w10i5qTzgFG"}
State: %{components: {%{1 => {UtmaktabaWeb.ModalComponent, :modal, %{__changed__: %{}, component: UtmaktabaWeb.VisitorLive.FormComponent, flash: %{}, id: :modal, myself: %Phoenix.LiveComponent.CID{cid: 1}, opts: [id: :upload, title: "Import Visitors", action: :upload, uploads: %{__phoenix_refs_to_names__: %{"phx-Fseb4rMTYjuATwHG" => :visitors_file}, visitors_file: #Phoenix.LiveView.UploadConfig<accept: ".xls,.xlsx", auto_upload?: false, entries: [], errors: [], max_entries: 1, max_file_size: 8000000, name: :visitors_file, progress_event: nil, ref: "phx-Fseb4rMTYjuATwHG", ...>}, return_to: "/admin/visitors"], return_to: "/admin/visitors"}, %{__changed__: %{}, root_view: UtmaktabaWeb.VisitorLive.Index}, {32418010583157767378746343849255060140, %{}}}, 2 => {UtmaktabaWeb.VisitorLive.FormComponent, :upload, %{__changed__: %{}, action: :upload, flash: %{}, id: :upload, myself: %Phoenix.LiveComponent.CID{cid: 2}, return_to: "/admin/visitors", title: "Import Visitors", uploads: %{__phoenix_refs_to_names__: %{"phx-Fseb4rMTYjuATwHG" => :visitors_file}, visitors_file: #Phoenix.LiveView.UploadConfig<accept: ".xls,.xlsx", auto_upload?: false, entries: [], errors: [], max_entries: 1, max_file_size: 8000000, name: :visitors_file, progress_event: nil, ref: "phx-Fseb4rMTYjuATwHG", ...>}}, %{__changed__: %{}, root_view: UtmaktabaWeb.VisitorLive.Index}, {107684438911017609171355754527962087739, %{2 => {222003774085469888410314492609622821962, %{}}}}}}, %{UtmaktabaWeb.ModalComponent => %{modal: 1}, UtmaktabaWeb.VisitorLive.FormComponent => %{upload: 2}}, 3}, join_ref: "15", serializer: Phoenix.Socket.V2.JSONSerializer, socket: #Phoenix.LiveView.Socket<assigns: %{__changed__: %{}, current_user: #Utmaktaba.Accounts.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirmed_at: ~N[2022-01-06 06:51:31], confirmed_at_v: false, email: "admin@example.com", hashed_password: "$argon2id$v=19$m=131072,t=8,p=4$lj1qrlgvKhnl5SU7BmFCaA$+g0qkpYj4FsZsHbkY97NYkHG47NB8ZlbO+GTY67smEQ", id: 1, inserted_at: ~N[2022-01-06 06:51:31], role: "admin", updated_at: ~N[2022-01-06 06:51:31], ...>, flash: %{}, live_action: :upload, locale: "ar", page_title: "Import Visitors", search_changeset: #Ecto.Changeset<action: nil, changes: %{}, errors: [], data: %{}, valid?: true>, uploaded_files: [], uploads: %{__phoenix_refs_to_names__: %{"phx-Fseb4rMTYjuATwHG" => :visitors_file}, visitors_file: #Phoenix.LiveView.UploadConfig<accept: ".xls,.xlsx", auto_upload?: false, entries: [], errors: [], max_entries: 1, max_file_size: 8000000, name: :visitors_file, progress_event: nil, ref: "phx-Fseb4rMTYjuATwHG", ...>}, visitors: [%Utmaktaba.VisitTracking.Visitor{__meta__: #Ecto.Schema.Metadata<:loaded, "visitors">, birthd

The code is as follow

#live/visitor_live/index.ex

def mount(_params, _session, socket) do

    {:ok,
      socket
      # |> assign(....)
      |> assign(:uploaded_files, [])
      |> allow_upload(:visitors_file, accept: ~w(.xls .xlsx), max_entries: 1)
    }
  end

# live/visitor_live/index.html.ex

# some code ...
<%= if @live_action in [:new, :edit] do %>
  <%= live_modal @socket, UtmaktabaWeb.VisitorLive.FormComponent,
    id: @visitor.id || :new,
    title: @page_title,
    action: @live_action,
    visitor: @visitor,
    return_to: Routes.visitor_index_path(@socket, :index) %>
<% end %>

<%= if @live_action in [:upload] do %>
  <%= live_modal @socket, UtmaktabaWeb.VisitorLive.FormComponent,
    id: :upload,
    title: @page_title,
    action: @live_action,
    uploads: @uploads,
    return_to: Routes.visitor_index_path(@socket, :index) %>
<% end %>
# some code ...

#live/visitor_live/form_component.ex

# some code ..
def update(%{action: :upload} = assigns, socket) do
    {:ok,
      socket
      |> assign(assigns)
    }
  end
# some .....


#live/visitor_live/form_comoponent.html.ex
<%=if @action in [:upload] do %>
<div id="visitor-upload-form-wrapper" class="uploads my-20 p-5 bg-gray-200">
  <h3><%= dgettext("headers", "Upload visitors") %></h3>
  <form id="upload-form" class="mt-10" phx-submit="upload-visitors-file" phx-change="validate-upload" phx-target={@myself}>
    <%= live_file_input @uploads.visitors_file %>
  <button type="submit" class="btn btn-indigo"><%= dgettext("links", "Upload") %></button>
</form>
</div>
<% end %>

First 4 of 4 Posts Switch mode

sfusato

sfusato

You seem to be declaring the upload (allow_upload) in the LiveView, but actually use it in the form_component.html.ex. Move the allow_upload in the form_component as well.

smahi

smahi OP

@sfusato
The issue is resolved thanks to @chrismccord.
The crash of the upload handler is the cause of this issue.
Thank you every for the help.

StorageCluster

StorageCluster

@smahi what was the solution? I honestly can’t follow or parse the info correctly.

smahi

smahi OP

@StorageCluster
I my case the upload handler bows up because of an error that lead to the issue.
The solution is to check that the upload handler does not raise any error.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement