0

I'm pretty new to coding so forgive me if my code is unreadable or my question simplistic.

I am trying to create a little server application that (amongst other things) displays the properties of a neo4j node. I am using node.js, Express and Aseem Kishore's Node-Neo4j REST API client, the documentation for which can be found here.

My question stems from my inability to fetch the properties of nodes and paths. I can return a node or path, but they seem to be full of objects with which I cannot interact. I poured through the API documents looking for some examples of how particular methods are called but I found nothing.

Ive been trying to call the #toJSON method like, "db.toJSON(neoNode);" but it tells me that db does not contain that method. I've also tried, "var x = neoNode.data" but it returns undefined.

Could someone please help me figure this out?

//This file accepts POST data to the "queryanode" module
//and sends it to "talkToNeo" which queries the neo4j database.
//The results are sent to "resultants" where they are posted to
//a Jade view. Unfortuantly, the data comes out looking like 
// [object Object] or a huge long string, or simply undefined.



var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://localhost:7474');



function resultants(neoNode, res){
    // if I console.log(neoNode) here, I now get the 4 digit integer
    // that Neo4j uses as handles for nodes. 
    console.log("second call of neoNode" + neoNode);
    var alpha = neoNode.data; //this just doesn't work
    console.log("alpha is: " +alpha); //returns undefined
    var beta = JSON.stringify(alpha);

    console.log("logging the node: ");
    console.log(beta);// still undefined
    res.render("results",{path: beta});
    res.end('end');

}


function talkToNeo (reqnode, res) {
    var params = {
    };
    var query = [
    'MATCH (a {xml_id:"'+ reqnode +'"})',
    'RETURN (a)' 
    ].join('\n');
    console.log(query);

    db.query(query, params, function (err, results) {
        if (err) throw err;

        var neoNode = results.map(function (result){
            return result['a']; //this returns a long string, looks like an array,
                                //but the values cannot be fetched out
        });
        console.log("this is the value of neoNode");
        console.log(neoNode);
        resultants(neoNode, res);
    });

};


exports.queryanode = function (req, res) {
    console.log('queryanode called');
    if (req.method =='POST'){
        var reqnode = req.body.node;    //this works as it should, the neo4j query passes in
        talkToNeo(reqnode, res)         //the right value.
    }
}

EDIT

Hey, I just wanted to answer my own question for anybody googling node, neo4j, data, or "How do I get neo4j properties?"

The gigantic object from neo4j, that when you stringified it you got all the "http://localhost:7474/db/data/node/7056/whatever" urls everywhere, that's JSON. You can query it with its own notation. You can set a variable to the value of a property like this:

var alpha = unfilteredResult[0]["nodes(p)"][i]._data.data;

Dealing with this JSON can be difficult. If you're anything like me, the object is way more complex than any internet example can prepare you for. You can see the structure by putting it through a JSON Viewer, but the important thing is that sometimes there's an extra, unnamed top layer to the object. That's why we call the zeroth layer with square bracket notation as such: unfilteredResult[0] The rest of the line mixes square and dot notation but it works. This is the final code for a function that calculates the shortest path between two nodes and loops through it. The final variables are passed into a Jade view.

function talkToNeo (nodeone, nodetwo, res) {
    var params = {
    };
    var query = [
    'MATCH (a {xml_id:"'+ nodeone +'"}),(b {xml_id:"' + nodetwo + '"}),',
    'p = shortestPath((a)-[*..15]-(b))',
    'RETURN nodes(p), p' 
    ].join('\n');
    console.log("logging the query" +query);


    db.query(query, params, function (err, results) {
        if (err) throw err;
        var unfilteredResult = results;


        var neoPath = "Here are all the nodes that make up this path: ";
        for( i=0; i<unfilteredResult[0]["nodes(p)"].length; i++) {
            neoPath += JSON.stringify(unfilteredResult[0]['nodes(p)'][i]._data.data);
        }  


        var pathLength = unfilteredResult[0].p._length;
        console.log("final result" + (neoPath));
        res.render("results",{path: neoPath, pathLength: pathLength});
        res.end('end');

    });

};

1 Answers1

0

I would recommend that you look at the sample application, which we updated for Neo4j 2.0 Which uses Cypher to load the data and Node-labels to model the Javascript types.

You can find it here: https://github.com/neo4j-contrib/node-neo4j-template

Please ask more questions after looking at this.

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80