Search code examples
node.jsneo4jparametersneo4j-driver

Getting node by id and parameters with Neo4j-Driver for nodejs


I'm trying to return a specific node with the id (using neo4j-driver package within my nodejs server). I have the id of the node I'm searching for. So I used the parameters in the run method as showed bellow but I'm not getting any nodes.

session.run('MATCH (n:User)-[:OWN]->(a) WHERE id(n) = $id RETURN a',{id: idUser})

I checked and idUser-value is 128 and when i'm running this command, I get the right node.

session.run('MATCH (n:User)-[:OWN]->(a) WHERE id(n) = 128 RETURN a',{id: idUser})

Is there any one to make the first command working so I can get the node of the given id ?

Thanks


Solution

  • As it is written in the driver documentation:

    Number written directly e.g. session.run("CREATE (n:Node {age: {age}})", {age: 22}) will be of type Float in Neo4j. To write the age as an integer the neo4j.int method should be used... https://github.com/neo4j/neo4j-javascript-driver#write-integers

    So when you pass the identifier from the node.js, you need to convert it:

    session
      .run('MATCH (n:User)-[:OWN]->(a) WHERE id(n) = $id RETURN a', {
        id: neo4j.int(idUser)
      })
    

    or:

    session
      .run('MATCH (n:User)-[:OWN]->(a) WHERE id(n) = toInteger($id) RETURN a', {
        id: idUser
      })