簡體   English   中英

無法使用 neo4j 驅動程序從 nodejs 連接到 neo4j

[英]Unable to connect to neo4j from nodejs using neo4j driver

我正在使用 neo4j-driver 通過 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();

整個代碼大約在一周前工作,但現在遇到了這個問題。

我正在使用neo4j-driver通過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();

整個代碼大約一個星期前就工作了,但是現在遇到了這個問題。

如果有人遇到這篇文章,這可能會有所幫助。

不確定這是否是同一個問題,但我在 Mac 上遇到了類似的問題(我意識到最初的問題是關於 wsl),我無法從使用 neo4j 驅動程序的 nodejs 應用程序訪問本地主機上的 Neo4j 實例。 我可以使用 curl、postman 和我的瀏覽器進行連接,但不能使用 nodejs 應用程序。 我還可以將我的應用程序連接到我機器外的任何 neo4j 實例。 同樣的代碼在 Windows 上也能正常工作。

It turned out I couldn't hit anything running on localhost on a mac from neo4j using the neo4j driver or any http client packages like got, node-fetch, or axios meaning it wasn't specific to neo4j . 我發現問題出在 localhost 在帶有 ipv6 的 mac 上的解析方式上 直接使用 127.0.0.1 而不是 localhost 解決了我的問題。

以下帖子非常有幫助: https://stackoverflow.com/a/15244890/1028560

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM