I'm certain that this code is a mess as I have never had to work with an API call such as this. The issue I'm facing is, in a production environment, node is not waiting for the axios call to complete before rendering the page.
I have no issues running it in the dev environment.
router.get('/:id', middleware.isLoggedIn, (req, res) => {
var perPage = 9
var page = req.params.page || 1
User.findById(req.params.id)
.populate({
path : 'log',
options : {
sort : { date : -1 },
limit: 8,
skip: (perPage * page) - perPage
},
}).exec((err, foundUser) => {
if(err){
req.flash('error', 'Something went wrong. User not found.');
res.redirect('back');
} else {
Logbook.find({author: req.user.id}).countDocuments().exec((err, count) => {
if (err) return next(err)
axios.get('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=' + foundUser.username + '&format=json')
.then(async response => {
var json = response.data;
if (json == undefined || json.Licenses == undefined || json.Licenses.License == undefined) {
if (err) {
console.log(err);
}
console.log('error1');
res.redirect('/profile/' + foundUser.id);
} else {
var data = json.Licenses;
var data2 = data.License;
var lic = [];
for (var i = 0; i < data2.length; i++) {
var lic = data2[i];
}
res.render('profile/index', {
user: foundUser,
json: lic,
current: page,
pages: Math.ceil(count / perPage),
logCount: count,
});
};
})
.catch(error => {
console.log(error);
res.redirect('/profile/' + foundUser.id);
});
});
}
});
Any help is sincerely appreciated!