如何将neo4j驱动程序集成到node.js中

时间:2019-03-31 13:54:13

标签: node.js neo4j

我想在节点上使用neo4j图形数据库。

但是我遇到了错误。请帮忙。

错误说未定义neo4j。 但是我已经使用npm安装了驱动程序

这是我的代码:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');

var app = require('neo4j-driver').v1;   // neo4J driver


var app = express();


app.set('views',path.join(__dirname,'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(path.join(__dirname,'public')));

// var driver =  neo4j.v1.driver('bolt://localhost:7687', neo4j.v1.auth.basic('neo4j','test'));  // neo4j driver
// var session = driver.session();

const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', 'test'));

app.get('/',function(req,res) {
        res.send('it Works');
});
app.listen(3000);
console.log('Server Started on port 3000');

module.exports = app;

我遇到以下错误:

PS D:\Projects\neo4J_node> node .\app.js
D:\Projects\neo4J_node\app.js:23
const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', 'test'));
               ^

ReferenceError: neo4j is not defined
    at Object.<anonymous> (D:\Projects\neo4J_node\app.js:23:16)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
PS D:\Projects\neo4J_node>

1 个答案:

答案 0 :(得分:0)

驱动程序已更新,您正在使用的驱动程序不再有效。在下面进行更改,并通过将测试节点添加到neo4j的会话进行测试。 更改:

var app = require('neo4j-driver').v1;   // neo4J driver

收件人:

var neo4j = require('neo4j-driver')

要测试连接,请尝试以下代码:

const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', 'test'));

const session = driver.session()

const resultPromise = session.writeTransaction(tx =>
  tx.run(
    'CREATE (a:Greeting) SET a.message = $message RETURN a.message + ", from node " + id(a)',
    { message: 'hello, world' }
  )
)

resultPromise.then(result => {
  session.close()

  const singleRecord = result.records[0]
  const greeting = singleRecord.get(0)

  console.log(greeting)

  // on application exit:
  driver.close()
})