0

I tried all the solutions posted here, but nothing worked, because my problem is a bit different.

I have the following code (calls on a code from external neo4j module to insert a node into Neo4J database and then gets the Neo4J Id of that node):

dbneo.insertNode({
    auth_id: user.id,
    username: user.name,
    name: user.name
},function (err, node){
    if(err) throw err;
    // Output node properties.
    console.log(node.data);
    // Output node id.
    console.log(node.id);
});

All works fine, but how do I deliver node.id outside of the scope of function (err,node) to then add this value to another object?

When I try to do something like

user.neo_id = node.id;

after that block of the code above, it's undefined (of course).

Tried to run the dbneo.insertNode inside a named function, but if I put the return value into function (err, node) it's undefined anyway.

Sorry if the question is a bit simple/stupid, but I'm just starting in JavaScript. Thank you!

Aerodynamika
  • 7,883
  • 16
  • 78
  • 137
  • not, not really... doesn't work for me... – Aerodynamika Feb 05 '14 at 16:59
  • 1
    you should either use `async` module to achieve this, combine multiple calls and have one result. or entirely depend on events which is inconvenient – Gntem Feb 05 '14 at 17:03
  • is there no easier way of just taking out that node.id variable from the function into the context? i mean it's such a small thing, surely there should be a way, no? :) – Aerodynamika Feb 05 '14 at 17:10

1 Answers1

0

Ok, so after your advice, I have this interim code. Probably not the best (may clog), but it resolves the problem.

I simply call for the next function only after I'm sure I received the variable (node.id) from Neo4J database. If anyone knows a more elegant solution, please, do let me know (especially with async library).

 dbneo.insertNode({
    auth_id: user.id,
    username: user.name,
    name: user.name
},function (err, node){
    if(err) throw err;
    // Output node properties.
    console.log(node.data);
    // Output node id.
    console.log(node.id);

    // RECEIVED node.id ? ONLY THEN SEND IT TO THE FUNCTION THAT NEEDS IT
    send_neo_id(node.id);
});


// THIS IS THE FUNCTION THAT WAS NOT RECEIVING NODE.ID BEFORE:

function send_neo_id(neo_id) {
    user.neo_id = neo_id;
    db.set('user:id:' + user.name, id, function(err) {
        if (err) return fn(err);
        db.hmset('user:' + id, user, function(err) {
            fn(err);
        });
    });
};
Aerodynamika
  • 7,883
  • 16
  • 78
  • 137