Neo4j浏览器中子图查询报错:变量TomH未定义的原因咨询
Variable TomH not defined Error Occurs Hey there! Let's break down exactly why you're seeing that error when running those final two lines on their own.
The Root Cause
That TomH variable you're using in the WITH clause is only valid within the context of the full 500+ line CREATE query. Here's what's happening:
- Buried in those hundreds of CREATE statements is a line that defines
TomHas a temporary alias for the Tom Hanks node (something likeCREATE (TomH:Person {name: 'Tom Hanks'})). - Neo4j variables are query-scoped—they only exist for the duration of the single query they're created in. Once that full CREATE script finishes running, the
TomHvariable gets discarded entirely. - When you run those final two lines alone, there's no
TomHvariable present in your current session context, so Neo4j throws the "not defined" error because it has no idea what you're referencing.
The Simple Fix
Since you've already created the full movie graph database with the complete tutorial script, you don't need to rely on that temporary variable anymore. Instead, directly target the Tom Hanks node using its persistent label and property:
MATCH (a:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m)<-[:DIRECTED]-(d) RETURN a, m, d LIMIT 10;
This query finds the node by its Person label and name property (which are stored in the database permanently), so it works independently of any temporary variables from earlier queries.
A Quick Cypher Variable Reminder
Always keep in mind: Variables in Cypher are just short-term aliases for nodes or relationships during a single query. They never get saved to the database, so you can't reference them across separate queries. For follow-up queries, always use labels, properties, or relationship types to target the data you need.
内容的提问来源于stack exchange,提问作者bjarkemoensted

