express - When to call next() in node.js when using Firebase to fetch data -


there easy solution problem, cannot seem wrap head around problem.

the problem is: using node.js express.js framework app creating. using firebase.js database. starting understand middleware , how can use fetch data, before sending response client.

my problem however, if want loop through child nodes in firebase, want use datasnap.foreach() so:

var scoresref = db.ref("scores");     scoresref.orderbyvalue().on("value", function(snapshot) {     snapshot.foreach(function(data) {     console.log("the " + data.key + " dinosaur's score " + data.val());   }); }); 

so lets have module should dinosaurs value, this:

var dinomodule = {}; dinomodule.getdinosaurs = function(req, res, next){     var dinoref = firebase.database().ref("dinosaurs");     dinoref.orderbyvalue().on('value', function(snapshot){         snapshot.foreach(function(data){           // data here           // calling next() here wrong         });       // should call next() here?     }); } 

but should call next function server not halt?

do know promises in nodejs? need use promises here solve problem.

var async = require('async'); var dinomodule = {}; dinomodule.getdinosaurs = function(req, res, next){     var dinoref = firebase.database().ref("dinosaurs");     dinoref.orderbyvalue().on('value', function(snapshot){         async.eachseries(snapshot,fucntion(data,callback){           //call callback() here after success           //in case of errors call callback(err) err error object         },function(err){             if(err){              //this called in case of error             }else{              //this called success after interation on              //call next() here             }           })     }); } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -