Can't access key in params even though it is in there in log

Hey I have this params arriving to my definition and I am calling it recording_params:

%{
  "challenge_id" => "2",
  "path_to_recording" => %Plug.Upload{
    content_type: "audio/mp4",
    filename: "2_a_Mister X.mp4",
    path: "/tmp/plug-1537/multipart-1537727280-384028641338291-1"
  },
  "user_id" => "5"
}

then I am trying to use it like this:

recordings =
  get_recording_list_by_user_id_and_challenge_id(
    recording_params.user_id,
    recording_params.challenge_id
  )

on this line recording_params.user_id I am getting an erro that says:

(KeyError) key :user_id not found in: %{"challenge_id" => "2", "path_to_recording" =>     %Plug.Upload{content_t
ype: "audio/mp4", filename: "2_a_Mister X.mp4", path: "/tmp/plug-1537/multipart-1537727280-384028641338291-1"}, "us
er_id" => "5"}

why is that? and how can i fix it?

The key user_id is not an atom but a string, so you can’t use the notation recording_params.user_id.

Use Map.fetch!(recording_params, "user_id") or recording_params["user_id"].

3 Likes

Dot-based syntax is only supported for atom keys. More info at https://hexdocs.pm/elixir/Access.html#module-dot-based-syntax

3 Likes

recording_params.user_id works only if the keys are atoms.

You can make your code work, by using recording_params["user_id"]

Your code should look like this:

recordings =
  get_recording_list_by_user_id_and_challenge_id(
    recording_params["user_id"],
    recording_params["challenge_id"]
  )

You can find more about this behaviour here: https://hexdocs.pm/elixir/Map.html (around paragraph nr 6 :slight_smile: )

4 Likes

thanks all :smile:

1 Like

Oh, thanks guys :grin:

1 Like