我使用新4j驱动程序通过nodejs连接到neo4j,但我面临一个问题。
即使数据库已启动和运行,并且可以通过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'
}驱动程序连接设置为
const neo4j = require('neo4j-driver');
const driver = neo4j.driver('bolt://localhost', neo4j.auth.basic('neo4j', 'password'));
module.exports = driver;我在不同的文件中使用这个导出的驱动程序,这些文件用于添加数据。
这是我用来向数据库添加数据的代码。
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();
整个代码一周前就已经开始工作了,但现在却遇到了这个问题。
发布于 2020-11-17 12:52:45
当我从wsl 2切换到时,这个问题实际上得到了解决。
发布于 2022-09-19 18:35:55
如果有人看到这篇文章,这可能会有帮助。
不确定这是否是同样的问题,但我在mac上遇到了类似的问题(我知道最初的问题是关于wsl的),在这个问题上,我无法使用Neo4j驱动程序从nodejs应用程序连接到本地主机上的一个neo4j实例。我可以使用curl、postman和我的浏览器进行连接,但不能使用nodejs应用程序。我还可以将我的应用程序连接到机器之外的任何neo4j实例。同样的代码在Windows上也运行得很好。
事实证明,我无法连接任何东西,无论是neo4j,甚至是使用neo4j驱动程序或任何http客户端包(如节点获取或axios )在本地主机上运行的简单web服务器,这意味着并不是neo4j特有的。正如我发现的那样,的问题在于如何在带有ipv6的mac上解决本地主机的问题。使用127.0.0.1直接代替本地主机的解决了我的问题。
https://stackoverflow.com/questions/64831589
复制相似问题