Node.js + Express - Callbacks returning undefined before Complete
You're returning your result as the first argument of your callback, but it's expected to be the second - in other words you're populating the err
argument with your result, so the argument result
will always be undefined
.
So change:
function functionOne(callback) { //QUERY TAKES A LONG TIME client.query("[QUERY]", function(err, result) { callback(result); });}
To this:
function functionOne(callback) { //QUERY TAKES A LONG TIME client.query("[QUERY]", function(err, result) { callback(null, result); // pass it as the second argument });}
That should solve your issue.
this should work:
//start execution functionOne (functionTwo); function functionOne(callback) { //QUERY TAKES A LONG TIME client.query("[QUERY]", function(err, result) { callback(null, result); }); } function functionTwo(err, result) { //call function done after long processing is finished with result done(err,result); } function done(err,result){ //do your final processing here console.log(result); }
just change
client.query("[QUERY]", function(err, result) { callback(null, result); });
to :
client.query("[QUERY]", function(err, result) { if( err) return callback(err); return callback(null, result); });