ExecuteRead() Returning nothing while cypher in browser work

query := `MATCH (n) RETURN n` 
	
	session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "test"})
	defer session.Close(ctx)
	
	courses, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
		result, err := tx.Run(ctx, query, nil)
		if err != nil {
			return nil, err
		}
	
		summary, err := result.Consume(ctx)
		if err != nil {
			return nil, err
		}
		fmt.Println(summary.Counters().NodesCreated())
		fmt.Println(summary.Counters().RelationshipsCreated())
	
		courses, err := result.Collect(ctx)
		if err != nil {
			return nil, err
		}
		return courses, nil
	})
	
	if err != nil {
		panic(err)
	}
	
	fmt.Println("done")
	
	t := courses.([]*neo4j.Record)
	
	if len(t) == 0 {
		fmt.Println("No records")
	}
	
	for _, r := range t {
		fmt.Println(r.Keys)
		fmt.Println(r.Values)
		fmt.Println(r.AsMap())
	}

ExecuteWrite() is working, can create node via code but retrevial part is not working, am i doing something wrong

update, session.Run() is working too only with session.ExecuteRead(). my driver version is v5.9.0 and database is 5.9

update: found the issue

summary, err := result.Consume(ctx)
		if err != nil {
			return nil, err
		}
		fmt.Println(summary.Counters().NodesCreated())
		fmt.Println(summary.Counters().RelationshipsCreated())

this part is causing the problem, I only need it for my ExecuteWrite() code and didn't remove it thinking it won't cause problem. but it did.
I will close this post but will be appreciated if anyone could explain why it cause it to return empty and throw no error.

Hello, result.Consume consumes all the results from the cursor, as explained in the docs:

Consume discards all remaining records and returns the summary information
about the statement execution.

So this is a terminal operation.
result.Collect called afterwards won't have any records left to fetch.

By the way, since you're executing a single query, have you tried this new API instead?

result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)

This gives you access to all records in memory, as well as the result summary.
You can see a full example in the README: GitHub - neo4j/neo4j-go-driver: Neo4j Bolt Driver for Go

1 Like

thanks you for the explanation, i didn't read the doc properly when i was calling. will give it a try. thanks you so much

1 Like