简体   繁体   中英

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.

It gives the error failed to connect to server even when the database is up and running and can be accessed via neo4j browser.

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.

It gives the error failed to connect to server even when the database is up and running and can be accessed via neo4j browser.

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. I could connect using curl, postman, and my browser, but not the nodejs app. I could also connect my app to any neo4j instance outside my machine. The same code also worked just fine on 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 . As I found out the problem was with the way localhost was resolving on a mac with ipv6 . Using 127.0.0.1 directly instead of localhost fixed my problem.

The following post was very helpful: https://stackoverflow.com/a/15244890/1028560

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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