parikshit
How to get access values from boltsips response data in .eex file
Want to access name and email filed in .eex file
How to iterate thorough Bolt.Sips.Response --> results --> properties --> email,name
%Bolt.Sips.Response{ bookmark: "", fields: ["n"], notifications: [], plan: nil, profile: nil, records: [ [ %Bolt.Sips.Types.Node{ id: 0, labels: ["E"], properties: %{ "email" => "abc@email.com", "name" => "Abc Xyz", } } ] ], results: [ %{ "n" => %Bolt.Sips.Types.Node{ id: 0, labels: ["E"], properties: %{ "email" => "abc@email.com", "name" => "Abc Xyz", } } } ], stats: [], type: "r" }
Marked As Solved
kokolegorille
I didn’t get the structure right. it should be…
Enum.map(results, fn m ->
%{n: m["n"], email: m["n"].properties["email"], name: m["n"].properties["name"]}
end)
Also Liked
Florin
you can use @kokolegorille’s example, and you can also use the recommendation from the Bolt.Sips Response docs and examples, pasting some samples below, for brevity:
iex» %Bolt.Sips.Response{results: results} = Bolt.Sips.query!(Bolt.Sips.conn(), "RETURN [10,11,21] AS arr")
iex» results
[%{"arr" => [10, 11, 21]}]
And of course, since Response implements the Enumerable protocol, you can easily use it for manipulating your results. Example:
Bolt.Sips.conn()
|> Bolt.Sips.query!("RETURN [10,11,21] AS arr")
|> Enum.reduce(0, &(Enum.sum(&1["arr"]) + &2))
# => 42
HTH
__
One of the main reasons the Response has a mix content, atoms and strings, is because we cannot control the response from the server, converting everything to atoms would obviously not be safe.
kokolegorille
Hello and welcome,
Probably the nicest way is to use
https://hexdocs.pm/elixir/Kernel.html#get_in/2
You need to remember results is a list. and your response mixes atoms and strings keys.
Florin
thank you @kokolegorille
kokolegorille
These are some rules when trying to access data, with mixed keys…
- If it is a list, use Enum.map to collect elements.
- If it is a map with string keys, You can get the value with map[“key”]
- If it is atom keys, You have the sugar… map.key, but if the key is not present, it fails
- In this case, You can use map[:key], it will not fail
- Use get_in if You have structs
- If it is a tuple, use elem/2
and just follow the path of your data 







