node.js - Pass function to next function, fire depending on condition. nodejs -
im using express.js create node.js rest server, part o creating simple session system. have 3 modules:
- app.js
- highscoreman.js
- usersession.js
the app.js url http://localhost/api/highscores calls usersession given parameters:
//get highscores app.get('/api/highscores', function (req, res) { usersession.checkvalidity(req.query['username'], req.query['sessionid'], highscoreman.getall(req, res)); }); however, in checkvalidity function pass automatically called:
function checkvalidity(username, sessionid, callback) { usersession.findone({ userid: username, sessionid: sessionid }, function (err, result) { if (err) { console.log(err); } if(result) { callback; } }); } i want run function being passed given proper results database(other checks added later session dates etc.). how this?
to delay calling highscoreman.getall(), you'll need make statement of function can called later:
app.get('/api/highscores', function (req, res) { usersession.checkvalidity(req.query['username'], req.query['sessionid'], function () { highscoreman.getall(req, res); }); }); otherwise, it's being called , return value instead being passed usersession.checkvalidity().
note you'll need adjust checkvalidity call passed callback:
// ... if(result) { callback(); } // ...
Comments
Post a Comment