0

for my Bachelor thesis I am required to build a Web frontend/ visualisation for a business process, using javascript. This process is stored inside a neo4j database (version 2.1.7) and I am using meteor together with the driver, which is recommended on neo4j's website (version 2.10).

Since I am fairly new to Web development in general and especialy to javascript, I am not sure if my problem is of a technical or knowledge (aka me being a beginner) nature.

At the moment I am working on a function, which should give me back a node from the database (currently "a node" equals a node's id, since the final representation of a node on the webpage is not yet decided).

getNodeById = function getNodeById(id){
    console.log("The given id is " + id);  
    var result = undefined;

    N4JDB.getNodeById(id, function getNode(node) {

      console.log ("The node's id is " + node.id);
      console.log("Result before " + result );
      result = node.id;
      console.log("Result after " +result);

    });
return result;

} 

Calling getNodeById(4) gives me the following output on meteor's console:

I20150309-16:03:32.200(1)? The given id is 4 I20150309-16:03:32.272(1)? The node's id is undefined I20150309-16:03:32.272(1)? Result before undefined I20150309-16:03:32.272(1)? Result after undefined

The problem right now is, that I am not sure how to write a callback function, which fits for the drivers getNodeById function.

Link to the driver's documentation: https://atmospherejs.com/ostrio/neo4jdriver

Edit:Further debugging has lead me to believe, that I make a mistake somewhere/how when invoking N4JDB.getNodeById since it always returns null

New function (require statement on top of the file, since there will be many functions similar to this one)looks like this

getNodeById = function getNodeById(id){
  var myFuture = new Future ();
  var result = undefined;
  N4JDB.getNodeById(id, function getNode(node){
      console.log(node);
      result = node.id;
      myFuture.return(result);
    });
  return myFuture.wait();
}

Final edit: It worked with

   getNodeById = function getNodeById(id){
      var myFuture = new Future ();
      var result = undefined;
      N4JDB.getNodeById(id, function getNode(err,node){
          //console.log(node);
          result = node.id;
          myFuture.return(result);
        });
      return myFuture.wait();

}

2 Answers2

0

Meteor is based to node.js, so it works asynchronously. Your code starts to execute the function, but not waits for the result.

A good solution can be use the fibers/future npm package, as is explained at this link. https://www.discovermeteor.com/patterns/5828399

So:

getNodeById = function getNodeById(id){
    Future = Npm.require('fibers/future');
    var myFuture = new Future();
    var result = undefined;

    N4JDB.getNodeById(id, function getNode(node) {
        result = node.id;
        myFuture.return(result);
    });

    return myFuture.wait();

} 

Hope it helps

delvedor
  • 52
  • 1
  • 10
  • I tried this, but it seems incompatible with something in my meteor/mongodb files, since it caused the error described here http://stackoverflow.com/questions/15610385/meteor-unexpected-mongo-exit-code-100 – Felix Huber Mar 10 '15 at 08:05
  • Did you install meteorhacks:npm and configured the packges.json? – delvedor Mar 10 '15 at 09:47
  • I have meteor and npm set up (to get the neo4j driver), but I did not do any further configuration. – Felix Huber Mar 10 '15 at 09:54
  • So you have the packages.json configured in this way? { "future": "2.3.1", "neo4j": "2.0.0-alpha5" } And you are using neo4j in this way? var neo4j = Meteor.npmRequire('neo4j'); – delvedor Mar 10 '15 at 10:04
  • I am using a different driver (https://atmospherejs.com/ostrio/neo4jdriver), using neo4j like this: this.N4JDB = new Meteor.Neo4j; – Felix Huber Mar 10 '15 at 10:08
0

In case you're using Meteor's Neo4jDriver, you can simply run and execute Cypher query.

var foundByID = new ReactiveVar(null);
N4JDB.query('MATCH (s) WHERE id(s) = {myNodeID} RETURN s;', {myNodeID: 123}, function(error, result){
   if(error){
       //Throw error here
       throw new Meteor.Error(500, error);
   }
   foundByID.set(result);
});

Or use Neo4jReactivity package

dr.dimitru
  • 2,645
  • 1
  • 27
  • 36