Cypher UNWIND returns local variable as undefined

Viewed 47

This works and returns the local variable:

const result = await session.writeTransaction(tx => {
      return tx.run(
        'UNWIND $batch AS row\
        WITH "HELLO WORLD" AS temp\
        MERGE (block:Block{blockNumber: 1})\
        RETURN temp\',
        rows
      )
    });
console.log(result.records[0]);

However, this returns undefined:

const result = await session.writeTransaction(tx => {
      return tx.run(
        'UNWIND $batch AS row\
        WITH "HELLO WORLD" AS temp\
        MERGE (block:Block{blockNumber: row.block.blockNumber, createdAt: row.block.createdAt})\
        RETURN temp\',
        rows
      )
    });
console.log(result.records[0]);

Any ideas? I want to be able to return some locally defined variables (simplified in this example).

1 Answers

Your second Cypher query is actually invalid, trying to run it in Neo4j Browser returns:

Variable `row` not defined (line 1, column 33 (offset: 32))
"MERGE (block:Block{blockNumber: row.block.blockNumber, createdAt: row.block.createdAt})"
                                 ^

The reason is that the WITH clause changes your "scope", and row is not included in it. Try and change your WITH clause to:

...
WITH "HELLO WORLD" AS temp, row\
...
Related