Node.js connecting to Neo4j running the same code gives different results

Viewed 44

Running the apoc.setLabels code below, from Node.js, after first opening Neo4j database gives the expected result. If the nodes are deleted and the same code is run again, there is a different (second) result. Deleting those nodes and running the code again, from Node.js, gives the second result again. The idea is to rename the nodes with labels taken from the parameter nName. Is there a way to ensure that running the code again in Node.js, without restarting the database every time, will give the expected result not the second result?

// renameNodes.js

const neo4j  = require('neo4j-driver');
const driver = neo4j.driver("bolt://localhost", 
                            neo4j.auth.basic("neo4j", "neo4j"));
const session = driver.session();
const session1 = driver.session();
// start by creating four instances of the dummy, Tree class
// then, using apoc, rename the name (that is, label - node name) from
// one of the properties that has the desired label name.

session
    .run
    (
        "CALL apoc.create.node(['Tree'], {nName: 'Gum', title: 'Gum Tree'}) "
    )
    .then( ( ) =>
    {
      session
        .run
        (
           "CALL apoc.create.node(['Tree'], {nName: 'Fruit', title: 'Fruit Tree'}) "
       )
       .then( () => 
       {
           session
           .run
           (
               "CALL apoc.create.node(['Tree'], {nName: 'Pine', title: 'Pine Tree'}) "
           )
           .then( ( ) =>
           {
               session
                 .run
                 (
                    "CALL apoc.create.node(['Tree'], {nName: 'Green', title: 'Green Tree'}) "   
                 )

           })
       })
    })
    .then( ( ) =>
    {
        console.log('Hello');
        session1
          .run
          (
              "match (t:Tree) \
              call apoc.create.setLabels(t, [t.nName, 'Resource'] ) \
              yield node \
              remove node.nName \
              return node; "
          )
    })
    

After starting the database and running the code in Node.js the result is 4 nodes with the labels Gum, Fruit, Pine and Green (as expected) If these are deleted from the Neo4j database and the code is run in Node.js again, the result is labels Gum, Fruit and the then the last 2 nodes still have their original Tree label

0 Answers
Related