会话在Neo4j中使用Node js过期

时间:2017-03-26 02:58:34

标签: node.js neo4j

我查看了文档:

我使用这些命令创建了一个项目:

npm install --save neo4j-driver
nano index.js

写下这段代码:

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

// Create a driver instance, for the user neo4j with password neo4j.
// It should be enough to have a single driver per database per application.
var driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "123456"));

// Register a callback to know if driver creation was successful:
driver.onCompleted = function() {
    // proceed with using the driver, it was successfully instantiated
    console.log('successfully connected');
};

// Register a callback to know if driver creation failed.
// This could happen due to wrong credentials or database unavailability:
driver.onError = function(error) {
    console.log('Driver instantiation failed', error);
};

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session();
console.log(session);

session.run("CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})");

// Close the driver when application exits
driver.close();

运行node index.js时收到此错误消息:

  

驱动程序实例化失败{[错误:套接字挂断]代码:   'SessionExpired'}

我已探索过:

我在123456首次发布后将密码设置为http://localhost:7474/browser/

我在Windows 10.防火墙方面我需要做些什么?我错过了什么?

编辑:我正在使用Neo4j 3.1.2

1 个答案:

答案 0 :(得分:4)

问题是session.run函数是异步的并返回promise。

但是你关闭会话直到它被执行并准备返回结果。

试试这个:

var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost:7687",
                          neo4j.auth.basic("neo4j", "123456")
);

driver.onCompleted = function() {
    console.log('successfully connected');
};
driver.onError = function(error) {
    console.log('Driver instantiation failed', error);
};

var session = driver.session();
console.log(session);    
session
    .run(`
        CREATE (TheMatrix:Movie { title:'The Matrix', 
                                  released:1999, 
                                  tagline:'Welcome to the Real World'
        })
        RETURN TheMatrix
    `)
    .then( function(result) {
        console.log(result);
        driver.close();
    })
    .catch( function(error) {
        console.log(error);
        driver.close();
    })