Neo4j节点驱动程序-连接被拒绝,我做错了什么?

6qftjkof  于 2023-03-12  发布在  其他
关注(0)|答案(2)|浏览(386)

我有一个简单的节点服务器(基于此处提供的文档-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)
        }
      })

我得到这个错误

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
  }
}

我可以从Neo4j浏览器查询数据库没有问题。你能帮帮我吗(新手)。提前感谢。
node server.js,并希望在控制台上打印查询结果。

8zzbczxx

8zzbczxx1#

当使用Neo4j community edition服务器时,在驱动程序的连接URI中使用bolt方案会更有意义。neo4j方案将尝试执行路由,这对于独立服务器是不需要的(尽管它仍然应该工作)。
因此,请尝试将neo4j://localhost替换为:bolt://localhost .即使你仍然得到一个错误,它应该是一个不同的。
另外,如果您不知道,如果您的客户端代码与服务器不在同一台机器上运行,localhost将无法工作。如果您的客户端在不同的机器上,则需要使用服务器的地址,而不是localhost

kgqe7b3p

kgqe7b3p2#

经过大量大量的研究,我能够找到一个解决方案,解决方案是使用session.executeRead而不是session.run,谢谢,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());

注:

  • 默认情况下,Neo4j驱动程序使用端口“7687”,因此不需要显式提供该端口
  • 如果未提供数据库名称,它将自动采用Neo4j db中的默认DB

相关问题