Reputation: 93
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();
Upvotes: 8
Views: 4264
Reputation: 42
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.
Upvotes: 0
Reputation: 1575
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
Upvotes: 12
Reputation: 93
The issue actually got resolved when I switched from wsl 2 to windows powershell.
Upvotes: -2