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

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
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
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

We're in Beta

About us Mission Statement