I trigger a function using require() in my app.js file and the result returned is 'undefined'. I think this is due to the function being nested. What is the best way to return the nested value. I have tried the method below but the function appears to be asynchronous and the returned value is returned as undefined before the data is provided.
app sample code --
app.get("/twitter/:handle", function (req, res) {
var handle = req.params.handle;
var twitter = require('./module/twitter');
var data = twitter.searchHandle(handle);
console.log(data);
res.sendStatus(data);
});
module sample code -
var twitter = {
searchHandle: function(handle){
var dataToReturn;
T.get('search/tweets', { q: handle, count: 100 }, function(err, data, response) {
dataToReturn = data;
});
return(dataToReturn);
}
};
module.exports = twitter;