cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to use session.run result as an iterator in different function after returning

with driver.session() as session:
      result = session.run("MATCH(n) RETURN n")
      return result

this result object is containing no data when tried to use in different function as an iterator but it works within same function. I am not sure if i am thinking or doing something wrong. please help me solve this

6 REPLIES 6

sameer_gijare14
Graph Buddy

Can you explain in detail what exactly you are trying to accomplish?

I want to use returned variable "result" in some other function like

for record in result:
   print(record)

Cobra
Ninja
Ninja

Hello @sandeepkrjha1999 and welcome to the Neo4j community

def bolt_to_list(result):
	"""
	Function to transform BOLT result into list of dictionnaries.
	"""
	return [r.data() for r in result]

So you can use this function like this:

with driver.session() as session:
      result = bolt_to_list(session.run("MATCH(n) RETURN n"))
      return result

Regards,
Cobra

even with this approach you are storing all results in a list at once in the memory, i don't want to do that, I want that i can use it as an iterator for accessing records one by one without storing it in any list.

Basically what i mean to say is I want to return the bolt type and then use it in some other function for accessing the records from the bolt iterator

glilienfield
Ninja
Ninja

The session is closed and resources released once leaving the 'with' clause. You need to consume the resource before leaving the with clause. You could call your print statement before returning.

rouven_bauer
Neo4j
Neo4j

Yes, if you want to iterate over the records one by one, you cannot close the session before you are done. What you could do it something like this:

with driver.session() as session:
    result = session.run("MATCH(n) RETURN n")
    for record in result:
        do_something_with_the_record(record)