I'm having some trouble figuring out what I need to add to allow for the get requests to finish and store the responses in twoDeeArray
so I can pass them to the html page. Currently the console.log(twoDeeArray)
at the bottom is returning an empty array. I thought Promise.all
would solve my problem and though it has fixed some issues I was having previously I'm still confused as to why the array is empty.
I don't think my schema info is necessary but just in case. I have two models, User and DoubleFeature. User contains a field called doublefeatures which stores tuples of movie ids that the user created a DoubleFeature for. It's a many to one relationship.
router.get('/:username', (req, res) => {
const username = req.params.username;
const doublefeatures = req.user.doublefeatures;
const twoDeeArray = [];
for (i = 0; i < doublefeatures.length; i++) {
DoubleFeature.findById(doublefeatures[i]).then(doublefeature => {
function getMovieOne() {
return axios.get(movieURL + doublefeature.movie_one_id + '?api_key=' + key);
};
function getMovieTwo() {
return axios.get(movieURL + doublefeature.movie_two_id + '?api_key=' + key);
};
Promise.all([getMovieOne(), getMovieTwo()])
.then(results => {
const movie_one = results[0].data;
const movie_two = results[1].data;
twoDeeArray.push([movie_one.title, movie_two.title]);
});
});
};
console.log(twoDeeArray);
res.render('users', {
'username': username
});
});