wlminimal
Drab: (KeyError) key :current_user not found in: %{}
Hi I am using drab to update chart js in a page
here is defhandler
defhandler show_dashboard_stats(socket, sender) do
order_id = sender.params["campaign_id"]
user_id = socket.assigns.current_user_id
order = Sales.get_order_by_id(order_id, user_id)
message_status = Messenger.get_all_message_status(order_id)
total_sent = Enum.count(message_status)
deilvered_count = Analytics.get_delivered_message_status_count(message_status)
undelivered_count = Analytics.get_undelivered_message_status_count(message_status)
bitly = Bitly.get_bilty_by_order_id(order_id)
total_clicks = Analytics.load_clicks(bitly)
poke socket,
total_sent: total_sent,
deilvered_count: deilvered_count,
undelivered_count: undelivered_count,
total_clicks: total_clicks,
recent_order: order
socket |> exec_js!("DashboardChart.update()")
end
and here is error message.
** (KeyError) key :current_user not found in: %{}
(texting) lib/texting_web/templates/dashboard/dashboard/index.html.drab:164: TextingWeb.Dashboard.DashboardView."index.html"/1
(phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/view.ex:339: Phoenix.View.render_to_string/3
(drab) lib/drab/live.ex:662: Drab.Live.rerender_template/4
(drab) lib/drab/live.ex:617: Drab.Live.process_poke/9
(texting) lib/texting_web/commanders/dashboard/dashboard_commander.ex:17: TextingWeb.Dashboard.DashboardCommander.show_dashboard_stats/2
(drab) lib/drab.ex:323: anonymous fn/7 in Drab.handle_event/6
And I don’t understand
** (KeyError) key :current_user not found in: %{}
in my app.html.eex I passed current_user id to drab like this
<%= Drab.Client.run(@conn, [current_user_id: @conn.assigns.current_user.id, order_id: @conn.assigns.recipients.id]) %>
So I inspected socket assigns in defhandler
then it shows correct user id.
So where does this error (KeyError) key :current_user not found in: %{} come from?
Almost same defhandler works in other page.
what is the possible cause?
Thanks
Marked As Solved
grych
So this is a conn case again.
Drab uses castrated version of conn, as it could be very heavy, especially when you keep a lot of data in assigns. See this issue for a reference. Drab creates it’s own version of conn, which by default copy only the few fields in private.
This behaviour is documented here. What you need is just to add something like :assigns => :current_user to :live_conn_pass_through config.
I know it is quite counter-intuitive, but I didn’t find any solution to change this, or at least tell user it is using the castrated conn version. Anyone?
Also Liked
NobbZ
You are relying on an implementation detail of structs, can’t you use proper syntax support instead:
iex(1)> defmodule S do
...(1)> defstruct a: 0
...(1)> end
iex(2)> %_{} = %S{}
%S{a: 0}
iex(3)> %_{} = %{}
** (MatchError) no match of right hand side value: %{}
OvermindDL1
Wait what?
How is this being hit:
https://github.com/grych/drab/blob/master/lib/drab/live/assign.ex#L66
When this is before it:
https://github.com/grych/drab/blob/master/lib/drab/live/assign.ex#L62
And that checks if it is a struct?! o.O
grych
Oh, exactly. How come?
This part of code hasn’t been changed for a while.
@wlminimal, please report it as a bug in github, I need to do more research.
Last Post!
grych
Popular in Questions
Other popular topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









