Unable to connect to neo4j from nodejs using neo4j driver
Asked Answered
A

3

8

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.
Abubekr answered 14/11, 2020 at 6:47 Comment(4)
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')Manchester
@LJRB No, the issue did not get resolved by adding :7687 at the end.Abubekr
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);Manchester
@LJRB even specifying encrypted=false the error persists.Abubekr
A
-2

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

Abubekr answered 17/11, 2020 at 12:52 Comment(2)
Issue occurs on debian as wellFrontispiece
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 answerModernize
G
12

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://mcmap.net/q/1327721/-inexplicable-node-js-http-throwing-connect-econnrefused-ipv6

Gentleman answered 19/9, 2022 at 18:35 Comment(0)
C
0

I tried connecting via neo4j:// and it connected successfully, after that I retried with bolt:// and it was working, not sure if this is a solution, but somehow it started working again.

Carmagnole answered 17/6 at 16:24 Comment(0)
A
-2

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

Abubekr answered 17/11, 2020 at 12:52 Comment(2)
Issue occurs on debian as wellFrontispiece
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 answerModernize

© 2022 - 2024 — McMap. All rights reserved.