从Node.js查询Neo4j时看不到任何回调

Unable to see any callback when querying Neo4j from Node.js

本文关键字:看不到 任何 回调 Neo4j Node js 查询      更新时间:2023-12-20

我从Node.js向Neo4j发送了一个查询,但没有看到任何回调。查询执行正确,但我无法在回调中看到任何信息并将其记录在控制台中。

我认为node.js会在任何数据到来之前执行console.log,但我不知道如何解决它

Node.js:

// Load Modules
var neo4j = require('neo4j');
// Database Connection
var db = new neo4j.GraphDatabase("http://neo4j:Gemitis26@localhost:7474/");
// Inizialize Query
var query = "CREATE (:Song {name:'James'})";
db.cypher(query, function(err, node){
    if(err) throw err;
    // Output node properties.
    console.log(node.data);
    // Output node id.
    console.log(node._id);
});

输出:

C:'Users'RRamos'Documents'Projects'test-neo4j>node index.js
[]
undefined

正如我所说,我检查了它,它是正确创建的。

您的代码中存在许多问题:

  1. Cypher查询没有RETURN子句,因此查询响应将始终为空数组(因为它永远不会包含任何结果行)。

  2. 您的回调预期响应的数据结构错误。

试试这个代码。它会转储错误(如果有的话)和响应,这样您就可以看到响应的实际数据结构。它还使用for循环来迭代响应中的数据行,并打印出每个s节点的属性及其本机ID。在您的情况下,最多只有一个结果行,因此循环不是严格必要的,但通常可以有多行。

// Load Modules
var neo4j = require('neo4j');
// Database Connection
var db = new neo4j.GraphDatabase("http://neo4j:Gemitis2@localhost:7474/");
// Inizialize Query
var query = "MATCH (s:Song {name:'James'}) RETURN s";
db.cypher(query, function(err, res){
    // Dump out the err and response, to see the data structure.
    console.log("err: %j, res: %j", err, res);
    if(err) throw err;
    // Print out the data for each row in the response.
    for (var i = 0; i < res.length; i++) {
        var s = res[i].s;
        // Output node properties.
        console.log(s.properties);
        // Output node id.
        console.log(s._id);
    }

});