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

时间:2022-11-12 18:05:02

I send a query from Node.js to Neo4j, but I do not see anything callback. The query is correctly executed but I am unable to see any information i nthe callback and log it in the console.

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

I think node.js executes console.log before any data has come, but I do not know how to solve it.

我认为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);
});

Output:

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

As I said, I check it and it is correctly created.

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

1 个解决方案

#1


0  

There are a number of problems in your code:

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

  1. Your Cypher query does not have a RETURN clause so your query response will always be an empty array (because it will never contain any result rows).

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

  2. Your callback is expecting to wrong data structure for the response.

    您的回调期望响应的数据结构错误。

Try this code. It dumps out the error (if any) and the response, so that you can see the actual data structure of a response. It also uses a for-loop to iterate through the rows of data in the response and print out each s node's properties and its native ID. In your case, there will only be at most one result row, so the loop is not strictly necessary, but in general there can be multiple rows.

试试这个代码。它会转出错误(如果有)和响应,以便您可以看到响应的实际数据结构。它还使用for循环遍历响应中的数据行,并打印出每个节点的属性及其本机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);
    }


});

#1


0  

There are a number of problems in your code:

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

  1. Your Cypher query does not have a RETURN clause so your query response will always be an empty array (because it will never contain any result rows).

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

  2. Your callback is expecting to wrong data structure for the response.

    您的回调期望响应的数据结构错误。

Try this code. It dumps out the error (if any) and the response, so that you can see the actual data structure of a response. It also uses a for-loop to iterate through the rows of data in the response and print out each s node's properties and its native ID. In your case, there will only be at most one result row, so the loop is not strictly necessary, but in general there can be multiple rows.

试试这个代码。它会转出错误(如果有)和响应,以便您可以看到响应的实际数据结构。它还使用for循环遍历响应中的数据行,并打印出每个节点的属性及其本机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);
    }


});