parikshit

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

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

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

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

Florin

thank you @kokolegorille

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 :slight_smile:

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New

We're in Beta

About us Mission Statement