7

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.
  • Hello, I'm not sure but maybe in const driver = neo4j.driver('bolt://localhost') it miss the 7687 port. Maybe try const driver = neo4j.driver('bolt://localhost:7687') – LJRB Nov 14 '20 at 09:22
  • @LJRB No, the issue did not get resolved by adding :7687 at the end. – Atharva Thorve Nov 14 '20 at 09:37
  • And if you add encrypted=False in const driver = neo4j.driver('bolt://localhost:7687') ? const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('neo4j', 'password'), encrypted=False); – LJRB Nov 14 '20 at 10:49
  • @LJRB even specifying encrypted=false the error persists. – Atharva Thorve Nov 14 '20 at 11:30

2 Answers2

9

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 connect to a Neo4j instance running on localhost 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 connect to anything, whether it's neo4j or even a simple web server running on localhost from neo4j using the neo4j driver or any http client packages like 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

Sanjeev
  • 1,517
  • 1
  • 18
  • 30
-2

The issue actually got resolved when I switched from wsl 2 to windows powershell.

  • Issue occurs on debian as well – til Jun 21 '22 at 10:15
  • 1
    this should not be marked as the solution, becasue it is not asolution to the problem, sut a way to avoid it. would be like "im having this problem on mac" and "switch to linux" was the answer – Bjamse Jul 20 '22 at 04:21