Skip to content Skip to sidebar Skip to footer

Return Variable Outside Of A Function Scope In Javascript

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

Solution 1:

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:functionsend_neo_id(neo_id) {
    user.neo_id = neo_id;
    db.set('user:id:' + user.name, id, function(err) {
        if (err) returnfn(err);
        db.hmset('user:' + id, user, function(err) {
            fn(err);
        });
    });
};

Post a Comment for "Return Variable Outside Of A Function Scope In Javascript"