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
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
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.
Popular in Questions
Other popular topics
Latest Phoenix Threads
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









