Greetings,
Recently I'm looking into the docs of Cypher and write a simple Cypher query in Golang,
I've tried to use multiple statements inside a single transaction.
but If I input a invalid id of children or parent and make the middle MATCH
statements failed on purpose, the transaction will not return anything for me (result.Next() is nil
). So the whole function will only return No data returned
in this way I cannot know which statement goes wrong. Any suggestions?
Thanks a lot!
_, err = session.WriteTransaction(func(transaction neo4j.Transaction) (interface{}, error) {
result, err := transaction.Run(
`CREATE (currentNode:Location $props)
WITH currentNode
OPTIONAL MATCH (children:Location) WHERE children.id IN $children
MERGE (currentNode)-[:contains]->(children)
WITH currentNode, children
OPTIONAL MATCH (parent:Location{id:currentNode.parent})
MERGE (parent) -[:contains]-> (currentNode)
WITH currentNode, parent, children
OPTIONAL MATCH path=(currentNode)-[contains*]-> (currentNode)
WHERE length(path) > 0
RETURN {cycle: length(path), children: children.id, parent: parent.id}`, map[string]interface{}{"props": input, "children": child})
if err != nil {
return nil, err
}
if result.Next() {
returnedMap := result.Record().GetByIndex(0).(map[string]interface{})
cycle := returnedMap["cycle"].(int64)
parent := returnedMap["parent"].(string)
children := returnedMap["parent"].(string)
if parent == "" {
errMsg := "Cannot find the given parent of the location"
return nil, errors.New(errMsg)
} else if children == "" {
errMsg := "Cannot find the given children of the location"
return nil, errors.New(errMsg)
} else if cycle > 0 {
errMsg := " Cycle Detected"
return nil, errors.New(errMsg)
}
} else {
return nil, errors.New("No data returned")
}
if result.Err() != nil {
return nil, result.Err()
}
return nil, nil
})