Neo4j node driver - Connection refused. What am I doing wrong?

603 Views Asked by At

I have this simple node server (based on the documentation provided here - https://www.npmjs.com/package/neo4j-driver?activeTab=readme

const neo4j = require('neo4j-driver');
const driver = neo4j.driver('neo4j://localhost', neo4j.auth.basic('admin', 'admin12345'));

const session = driver.session({
    database: 'neo4j',
    defaultAccessMode: neo4j.session.WRITE
})

session
    .run('MATCH (n) RETURN n')
    .subscribe({
        onKeys: keys => {
          console.log(keys)
        },
        onNext: record => {
          console.log(record)
        },
        onCompleted: () => {
          session.close() // returns a Promise
        },
        onError: error => {
          console.log(error)
        }
      })

I get this error

Neo4jError: Could not perform discovery. No routing servers available. Known routing table: RoutingTable[database=neo4j, expirationTime=0, currentTime=1678320189241, routers=[], readers=[], writers=[]]

    at captureStacktrace (/Users/j/Development/Neo4j/server/node_modules/neo4j-driver-core/lib/result.js:611:17)
    at new Result (/Users/j/Development/Neo4j/server/node_modules/neo4j-driver-core/lib/result.js:105:23)
    at Session._run (/Users/j/Development/Neo4j/server/node_modules/neo4j-driver-core/lib/session.js:223:16)
    at Session.run (/Users/j/Development/Neo4j/server/node_modules/neo4j-driver-core/lib/session.js:174:27)
    at Object.<anonymous> (/Users/j/Development/Neo4j/server/neo.js:10:6)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
  code: 'ServiceUnavailable',
  retriable: true,
  [cause]: Neo4jError: Failed to connect to server. Please ensure that your database is listening on the correct host and port and that you have compatible encryption settings both on Neo4j server and driver. Note that the default encryption setting has changed in Neo4j 4.0. Caused by: connect ECONNREFUSED ::1:7687
      at new Neo4jError (/Users/j/Development/j/Neo4j/server/node_modules/neo4j-driver-core/lib/error.js:77:16)
      at newError (/Users/j/Development/j/Neo4j/server/node_modules/neo4j-driver-core/lib/error.js:113:12)
      at NodeChannel._handleConnectionError (/Users/j/Development/j/Neo4j/server/node_modules/neo4j-driver-bolt-connection/lib/channel/node/node-channel.js:227:56)
      at Socket.emit (node:events:513:28)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    constructor: [Function: Neo4jError] { isRetriable: [Function (anonymous)] },
    code: 'SessionExpired',
    retriable: true
  }
}

I am able to query the db from the Neo4j browser without a problem. Can you please help me (a newbie). Thanks in advance.

node server.js and expected the query results to be printed on the console.

2

There are 2 best solutions below

0
cybersam On

When using a Neo4j community edition server, it makes more sense to use the bolt scheme in your driver's connection URI. The neo4j scheme will attempt to do routing, which is not needed for a standalone server (although it is still supposed to work).

So, try replacing neo4j://localhost with : bolt://localhost. Even if you still get an error, it should be a different one.

Also, in case you are not aware, localhost will not work if your client code is not running on the same machine as the server. If your client is on a different machine, then you need to use the address of the server instead of localhost.

0
JMS On

After lots and lots of research, I was able to find a solution. The solution is to use session.executeRead instead of session.run. Thank you, GraphAcademy - https://graphacademy.neo4j.com/courses/app-nodejs/2-interacting/2-results/

var neo4j = require('neo4j-driver');
var driver = neo4j.driver('neo4j://localhost', neo4j.auth.basic('admin', 'admin12345'));

var session = driver.session({ defaultAccessMode: neo4j.session.WRITE })

session.executeRead(q => q.run(
    'MATCH (n) RETURN n'
))
.then(res => {
    return res.records.map(row => {
        return row.get('n')
    })
})
.then( n => console.log(n))
.catch(e => console.log(e))
.finally(() => session.close());

Note:

  • By default, Neo4j driver assumes port '7687'. So there is no need to explicitly provide that
  • If no database name is provided, it will automatically assume the default DB in the Neo4j db