我正在使用neo4j-driver通过nodejs连接到neo4j,但我遇到了问题。
它会报错“无法连接到服务器”,即使数据库正在运行并可以通过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();