使用 Neo4j 驱动程序在 Node.js 中无法连接到 Neo4j 数据库。

7
我正在使用neo4j-driver通过nodejs连接到neo4j,但我遇到了问题。

它会报错“无法连接到服务器”,即使数据库正在运行并可以通过neo4j浏览器访问。
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 127.0.0.1:7687

    at captureStacktrace (/mnt/d/Codes/SIMply/server/node_modules/neo4j-driver/lib/result.js:277:15)
    at new Result (/mnt/d/Codes/SIMply/server/node_modules/neo4j-driver/lib/result.js:68:19)
    at Session._run (/mnt/d/Codes/SIMply/server/node_modules/neo4j-driver/lib/session.js:174:14)
    at Session.run (/mnt/d/Codes/SIMply/server/node_modules/neo4j-driver/lib/session.js:135:19)
    at /mnt/d/Codes/SIMply/server/database/randProviderdata.js:25:19
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'ServiceUnavailable',
  name: 'Neo4jError'
}

驱动程序连接设置为

const neo4j = require('neo4j-driver');

const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('neo4j', 'password'));

module.exports = driver;

我在不同的文件中使用导出的驱动程序来添加数据。
以下是我用于向数据库添加数据的代码。

const fs = require('fs');
const path = require('path');
const driver = require('./config');

const filePath = path.join(__dirname, 'providerdata.json');

const addData = async () => {
    fs.readFile(filePath, { encoding: 'utf-8' }, async (err, data) => {
        if (err) {
            console.log(err);
        }
        let session;
        try {
            session = driver.session();
            await session.run('MATCH (a:Provider) DETACH DELETE a');
            await session.close();
        } catch (error) {
            console.log(error);
        }
        const providerData = JSON.parse(data);
        for (let index = 0; index < providerData.length; index++) {
            const d = providerData[index];
            session = driver.session();
            try {
                await session.run('CREATE (a:Provider {name:$name,id:$id})', {
                    name: d.name,
                    id: d.id,
                });
                await session.close();
            } catch (error1) {
                console.log(error1);
            }
        }
    });
    await driver.close();
    console.log('done');
};

addData();


你好,我不确定但是也许在 const driver = neo4j.driver('bolt://localhost') 中缺少了 7687 端口。也许尝试 const driver = neo4j.driver('bolt://localhost:7687')。 - user13469785
@LJRB 不,通过在末尾添加:7687问题没有得到解决。 - Atharva Thorve
如果您在 const driver = neo4j.driver('bolt://localhost:7687') 中添加 encrypted=False,会发生什么呢? const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('neo4j', 'password'), encrypted=False); - user13469785
@LJRB 即使指定 encrypted=false,错误仍然存在。 - Atharva Thorve
2个回答

10
如果有人看到这篇帖子,这可能会很有帮助。 不确定是否是同样的问题,但我在Mac上遇到了类似的问题(我知道原始问题是关于wsl的),我的nodejs应用程序无法使用neo4j驱动程序连接到本地主机上运行的Neo4j实例。我可以使用curl、postman和浏览器连接,但不能使用nodejs应用程序连接。我还可以将我的应用程序连接到任何在我的机器之外的neo4j实例。相同的代码在Windows上也完美运行。
结果我无法使用neo4j驱动程序或任何http客户端包如node-fetch或axios从neo4j连接到任何东西,无论是neo4j还是运行在本地主机上的简单Web服务器,意味着它不仅仅与neo4j相关。后来我发现问题出在Mac上使用ipv6解析localhost的方式上。直接使用127.0.0.1而不是localhost解决了我的问题。
以下文章非常有帮助:https://dev59.com/-2_Xa4cB1Zd3GeqPyz2V#15244890

-2
问题实际上是在我从wsl 2切换到Windows Powershell后解决的。

问题在 Debian 上也出现了。 - til
1
这不应被标记为解决方案,因为它不是解决问题的方法,而是避免问题的方法。就像“我在Mac上遇到了这个问题”,然后“切换到Linux”就是答案。 - Bjamse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接