wlminimal

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

grych

Creator of Drab

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

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

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

grych

Creator of Drab

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

grych

Creator of Drab

Cool, I didn’t know this syntax, thanks!

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
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

We're in Beta

About us Mission Statement