hariharasudhan94

hariharasudhan94

Key not found - Map?

I am new to elixir devlopement , my map is looks like follow

map = %{"name" => "test"}

But when i trying to access map.name I am getting error like

 (KeyError) key :name not found in: %{"name" => "test"}

why i can’t get map value whose key is string

Marked As Solved

aseigo

aseigo

The map.keyname mechanism only works when atoms are used as keys. When using strings you can use Map.get:

iex(3)> Map.get(map, "name")
"test"

or the syntax:

iex(4)> map["name"]
"test"

So you could instead use atoms for keys:

iex(5)> map = %{:name => "test"}
%{name: "test"}
iex(6)> map.name
"test"

More info here in the docs, of course: Map — Elixir v1.20.2

hth…

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

Please do note that when you work with user input, you should always work with a map with string keys, and not atom keys, because if you auto-convert user input to atoms, you will open up your system to a Denial of Service attack because atoms are not garbage collected. Always use String.to_existing_atom (over String.to_atom) when you convert user input into atoms.

hariharasudhan94

hariharasudhan94

@Qqwy thanks for your valuable note

Last Post!

hariharasudhan94

hariharasudhan94

@Qqwy thanks for your valuable note

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
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

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement