cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

undefined variable error in WITH

jmrepka
Node Link
I'm stuck and confused. "WITH collect(s) AS newSymbols, size(newSymbols) AS nodeCount" throws an undefined variable error for "size(newSymbols AS nodeCount". I've seen this pattern in examples and I can "RETURN newSymbols, size(newSymbols)" with no problem. Why the error and how can I get around it? Help much, much appreciated.
 
//Symbols, add from doc(2)
WITH [$docStart] + split($doc,"") + [$docEnd] as symbols 
UNWIND symbols as symbol
MERGE (s:SYMBOL {name:symbol})
    ON CREATE SET s.count=1
    ON MATCH SET s.count = coalesce(s.count,0) + 1
    
WITH collect(s) AS newSymbols, size(newSymbols) AS nodeCount 
RETURN newSymbols, nodeCount
1 ACCEPTED SOLUTION

Then you can do something like this: 


WITH [$docStart] + split($doc,"") + [$docEnd] as symbols 
UNWIND symbols as symbol
MERGE (s:SYMBOL {name:symbol})
    ON CREATE SET s.count=1
    ON MATCH SET s.count = coalesce(s.count,0) + 1
    
WITH collect(s) AS newSymbols
WITH newSymbols, size(newSymbols) AS nodeCount 
CALL {
  with newSymbols, nodeCount
  //do processing
}
//do more processing...

or, you can pass them to a procedure with a 'call' statement.  

View solution in original post

4 REPLIES 4

You can't reference a variable that you define on the same line. Try this instead:


WITH [$docStart] + split($doc,"") + [$docEnd] as symbols 
UNWIND symbols as symbol
MERGE (s:SYMBOL {name:symbol})
    ON CREATE SET s.count=1
    ON MATCH SET s.count = coalesce(s.count,0) + 1
    
WITH collect(s) AS newSymbols
RETURN newSymbols, size(newSymbols) AS nodeCount 

Thanks for the quick reply. My problem is that I need to define nodeCount as as a variable together with newSymbols to use in a subsequent CALL to create a linked list using nodeCount and nodeCount + 1 to specify nodes from newNodes. I'm pretty new at Cypher and may be doing it all wrong.

Then you can do something like this: 


WITH [$docStart] + split($doc,"") + [$docEnd] as symbols 
UNWIND symbols as symbol
MERGE (s:SYMBOL {name:symbol})
    ON CREATE SET s.count=1
    ON MATCH SET s.count = coalesce(s.count,0) + 1
    
WITH collect(s) AS newSymbols
WITH newSymbols, size(newSymbols) AS nodeCount 
CALL {
  with newSymbols, nodeCount
  //do processing
}
//do more processing...

or, you can pass them to a procedure with a 'call' statement.  

Perfect.  I tried the two WITH statements, but got the second one wrong (failed to include newSymbols in the second one.) You showed my error exactly. Thanks for the time & trouble!