cancel
Showing results for 
Search instead for 
Did you mean: 

Neo4j NodeJS code doesn't display the nodes and its properties

somiya12
Node

I want to display all the nodes and its properties in json format using NodeJs 

But with this code it is showing error

app.get("/cloud/scompany/c", function (req, res) {

      session

      .run(` MATCH(N) RETURN N ;`)  
      .then(function (result){

          result.records.forEach(function(record){
              console.log("record = ", record);
              console.log("result = ", result)
              console.log("1] record._fields[0].properties = ",record._fields[0].end.properties);  
           //    res.send(record);          
          });
          res.send(result);

      })      
      .catch(function(err){
       console.log("inside catch = " + err);
   })

  })
1 ACCEPTED SOLUTION

You will need to change your query to get the related nodes. The following query matches on every node, then gets related nodes if the exists. It returns the data in a map, with keys 'node' and 'relatedNodes'. The node's value is a map of the node's properties and the relatedNodes is a list of property maps for each related node. 

match(n)
optional match (n)--(m)
with n as node, collect(properties(m)) as relatedNodes
return {node: node, relatedNodes: relatedNodes} as json

 Replace your query with the above cypher and use record.get("json"). The result should be a map that you can extract its key/value pairs using dot notation. 

If you don't want the json output, but the items individually, then use the following query and use record.get("node") to get the node properties and record.get("relatedNodes") to get a list of the related nodes' properties.

match(n)
optional match (n)--(m)
return n as node, collect(properties(m)) as relatedNodes

If this doesn't give you the format you want, please describe it so we can format the data appropriately. 

 

View solution in original post

5 REPLIES 5

glilienfield
Ninja
Ninja

Try removing the semicolon. If that doesn’t help, can you provide the error? 

somiya12
Node

After removing semicolon from cypher it still shows error

somiya12_0-1661694075267.png

 

It looks like it does not understand the following snippet:

record._fields[0].end.properties

I don't use the js driver, but from the documentation I could not find a reference to '_fields' property/method on the 'record' object. There is a 'keys' member that returns an array of the Field keys in the order the fields appear in the record. Where did you get this syntax?

Anyways, you should only have one field resulting from your query, since your return statement only has node 'N'. You can get this from the record using 'record.get("N")'.  After getting the node, you should be able to get its labels from 'node.labels' and properties from 'node.properties'. 

somiya12
Node

How should 'node.labels' and 'node.properties' should be added?

And after making changes to  'record.get("N")' it is only showing nodes and their data . In  JSON format it is not showing relations between nodes in form of array like structure

https://community.neo4j.com/t5/drivers-stacks/how-to-make-neo4j-nodejs-code-display-the-entire-nodes...

I want to display the data in similar format like in the above link

You will need to change your query to get the related nodes. The following query matches on every node, then gets related nodes if the exists. It returns the data in a map, with keys 'node' and 'relatedNodes'. The node's value is a map of the node's properties and the relatedNodes is a list of property maps for each related node. 

match(n)
optional match (n)--(m)
with n as node, collect(properties(m)) as relatedNodes
return {node: node, relatedNodes: relatedNodes} as json

 Replace your query with the above cypher and use record.get("json"). The result should be a map that you can extract its key/value pairs using dot notation. 

If you don't want the json output, but the items individually, then use the following query and use record.get("node") to get the node properties and record.get("relatedNodes") to get a list of the related nodes' properties.

match(n)
optional match (n)--(m)
return n as node, collect(properties(m)) as relatedNodes

If this doesn't give you the format you want, please describe it so we can format the data appropriately.