I am creating an API with ExpressJS and NodeJS, trying to wrap my head around callback. Somehow Callback is not returning the request result when called via Postman API. What might be the issue
router.get('/products', PipedriveController.pipedriveAllProducts)
// Export the router
module.exports = router;
var request = require('request');
var results;
function logRes(){
console.log(results);
return results
}
function readResult(callback){
request('https://jsonplaceholder.typicode.com/posts/1', function(err, response, body){
results=body;
callback();
});
}
exports.pipedriveAllProducts = function(req, res, next){
// let family = req.param.options;
try {
let all_products = readResult(logRes)
// Return All product liist with Appropriate HTTP header response
return res.status(200).json({status: 200, all_products});
} catch(e){
// Return an Error Response Message
return res.status(400).json({status: 400, message: e.message});
}
}
My Postman API output when i consume the API,
Result:
{
"status": 200
}
I should be expecting status 200 & the API JSON output