Search code examples
node.jsneo4jnode-neo4j

Nodejs with Neo4j: Sytax error on a multi - merge query


I have the following function that inserts a row of data into a row based-entry (excell or csv entries) into my neo4j:

  self.insertFromExcellRow=function(row){

      // _neo4j a neo4j connection from neo4j-driver
      const session = _neo4j.session();

      //The replacement of the values where it will get stored in neo4j
      const values={
        'dataid':row.dataId,
        'dataAsset':row.dataAsset,
        'dataSubject':row.dataSubject,
        'purpoce':row.purpoce,
        'source':row.source,
        'pIIclasification':row.pIIclasification,
        'securityClassification':row.securityClassification,
        'categoryInfo':row.categoryInfo,
        'appName':row.collectedBy,
        'securityControl':row.securityControl,
        'usedBy':row.usedBy,
        'whoCanAccess':row.access,
        'securityControl':row.securityControl,
        'dataTransferMechanism':row.dataTranserMechanism,
        'serviceName':row.collectedBy
      }

      let query=`MERGE (DATA_ASSET:DATA_ASSET {id:{dataid} ,name:{dataAsset}, subject:{dataSubject}, classification:{securityClassification})
      MERGE (SERVER_OR_SERVICE:SERVER_OR_SERVICE { name: {serviceName} })
      MERGE (APPLICATION:APPLICATION { name:{appName} })
      MERGE (DATA_CONSUMER:DATA_CONSUMER {usedBy: {usedBy}, accessOrgPositions: {whoCanAccess}})
      MERGE (SERVER_OR_SERVICE)<-[:FROM]-(DATA_CONSUMER)-[:ACCESSING]->(DATA_ASSET)
      MERGE (DATA_ASSET)-[:COLLECTED_BY]->(APPLICATION)
      `;

      if(row.processingType.toLowerCase()==='transmission') {
        query+=`MERGE (DATA_ASSET)-[:GETS]->(TRANSMITTED:TRANSMITTED { id:{dataid},purpoce:{purpoce},source:{source},pIIclasification:{pIIclasification},categoryInfo:{categoryInfo} })<-[:FROM]-(APPLICATION)
        MERGE (DATA_ASSET)-[:GETS]->(TRANSMITTED)-[:INTO { transferMechanism:{dataTransferMechanism}, securityControl:{securityControl}}]->(SERVER_OR_SERVICE)`;
      } else if(row.processingType.toLowerCase()==='storage') {
        query+=`MERGE (DATA_ASSET)-[:GETS]->(STORED:STORED { id:{dataid},purpoce:{purpoce},source:{source},pIIclasification:{pIIclasification},categoryInfo:{categoryInfo} })<-[:FROM]-(APPLICATION)
        MERGE (DATA_ASSET)-[:GETS]->(STORED)-[:INTO { transferMechanism:{dataTransferMechanism}, securityControl:{securityControl}}]->(SERVER_OR_SERVICE)`;
      }

      session.run(query,values).then((data)=>{
        console.log('Success');
        self.fetchDataAsTable();
      }).catch((error)=>{
        _emmiter.emit('insert_row_error',error);
        console.error(error);
      });
  }

But for some reason I get the error:

{ Neo4jError: Invalid input ')': expected whitespace, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, ',' or '}' (line 1, column 124 (offset: 123))
"MERGE (DATA_ASSET:DATA_ASSET {id:{dataid} ,name:{dataAsset}, subject:{dataSubject}, classification:{securityClassification})"
                                                                                                                             ^ 
at captureStacktrace (/opt/map/node_modules/neo4j-driver/lib/v1/result.js:200:15)
at new Result (/opt/map/node_modules/neo4j-driver/lib/v1/result.js:73:19)
at Session._run (/opt/map/node_modules/neo4j-driver/lib/v1/session.js:122:14)
at Session.run (/opt/map/node_modules/neo4j-driver/lib/v1/session.js:101:19)
at self.insertFromExcellRow (/opt/map/src/services/graph.js:67:15)
at /opt/map/src/services/excell.js:118:7
at process._tickCallback (internal/process/next_tick.js:150:11)
code: 'Neo.ClientError.Statement.SyntaxError',
name: 'Neo4jError' }

Do you fellows can figure out why?


Solution

  • looks like you need to insert a closing }

    MERGE (DATA_ASSET:DATA_ASSET {id:{dataid} ,name:{dataAsset}, subject:{dataSubject}, classification:{securityClassification} here )

    it closes the 'DATA_ASSET:DATA_ASSET' { .. }

    It would have been better if the error message said: "You need a closing curly brace" instead of throwing the dictionary at you - with the very last option being the one you need, lol..

    At least, it does have a nice '^' character to show you where the error is.