繁体   English   中英

无法使用 neo4j 驱动程序从 nodejs 连接到 neo4j

[英]Unable to connect to neo4j from nodejs using neo4j driver

我正在使用 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();

整个代码大约在一周前工作,但现在遇到了这个问题。

我正在使用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();

整个代码大约一个星期前就工作了,但是现在遇到了这个问题。

如果有人遇到这篇文章,这可能会有所帮助。

不确定这是否是同一个问题,但我在 Mac 上遇到了类似的问题(我意识到最初的问题是关于 wsl),我无法从使用 neo4j 驱动程序的 nodejs 应用程序访问本地主机上的 Neo4j 实例。 我可以使用 curl、postman 和我的浏览器进行连接,但不能使用 nodejs 应用程序。 我还可以将我的应用程序连接到我机器外的任何 neo4j 实例。 同样的代码在 Windows 上也能正常工作。

It turned out I couldn't hit anything running on localhost on a mac from neo4j using the neo4j driver or any http client packages like got, node-fetch, or axios meaning it wasn't specific to neo4j . 我发现问题出在 localhost 在带有 ipv6 的 mac 上的解析方式上 直接使用 127.0.0.1 而不是 localhost 解决了我的问题。

以下帖子非常有帮助: https://stackoverflow.com/a/15244890/1028560

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM