简体   繁体   English

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

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

I am using neo4j-driver to connect to neo4j via nodejs but I am facing an issue.我正在使用 neo4j-driver 通过 nodejs 连接到 neo4j 但我遇到了一个问题。

It gives the error failed to connect to server even when the database is up and running and can be accessed via neo4j browser.即使数据库已启动并运行并且可以通过 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'
}

The driver connection settings is驱动程序连接设置为

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

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

module.exports = driver;

I use this exported driver in different files which are used to add data.我在用于添加数据的不同文件中使用这个导出的驱动程序。
This is the code I am using to add data to the database.这是我用来向数据库添加数据的代码。

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();

This whole code was working around a week ago but now is running into this problem. 整个代码大约在一周前工作,但现在遇到了这个问题。

I am using neo4j-driver to connect to neo4j via nodejs but I am facing an issue.我正在使用neo4j-driver通过nodejs连接到neo4j,但是我遇到了一个问题。

It gives the error failed to connect to server even when the database is up and running and can be accessed via neo4j browser.即使数据库已启动并正在运行,它也会导致错误无法连接到服务器,并且可以通过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'
}

The driver connection settings is驱动程序连接设置为

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

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

module.exports = driver;

I use this exported driver in different files which are used to add data.我在不同的文件中使用了此导出的驱动程序,这些文件用于添加数据。
This is the code I am using to add data to the database.这是我用来向数据库添加数据的代码。

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();

This whole code was working around a week ago but now is running into this problem. 整个代码大约一个星期前就工作了,但是现在遇到了这个问题。

In case someone comes across this post this might be helpful.如果有人遇到这篇文章,这可能会有所帮助。

Not sure if this was the same problem, but I had a similar issue on a mac (I realize the original question was about wsl) where I couldn't hit a Neo4j instance on localhost with from a nodejs app using the neo4j drivers.不确定这是否是同一个问题,但我在 Mac 上遇到了类似的问题(我意识到最初的问题是关于 wsl),我无法从使用 neo4j 驱动程序的 nodejs 应用程序访问本地主机上的 Neo4j 实例。 I could connect using curl, postman, and my browser, but not the nodejs app.我可以使用 curl、postman 和我的浏览器进行连接,但不能使用 nodejs 应用程序。 I could also connect my app to any neo4j instance outside my machine.我还可以将我的应用程序连接到我机器外的任何 neo4j 实例。 The same code also worked just fine on Windows.同样的代码在 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 . 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 . As I found out the problem was with the way localhost was resolving on a mac with ipv6 .我发现问题出在 localhost 在带有 ipv6 的 mac 上的解析方式上 Using 127.0.0.1 directly instead of localhost fixed my problem.直接使用 127.0.0.1 而不是 localhost 解决了我的问题。

The following post was very helpful: https://stackoverflow.com/a/15244890/1028560以下帖子非常有帮助: https://stackoverflow.com/a/15244890/1028560

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

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