My goal is to fetch characters from swapi that played in the movie that I choose.
In the first route
router.get('/task1', movieController.getAllMovies)
I fetch all data about the movie:
exports.getAllMovies = (req, res, next) => {
axios.get('https://swapi.co/api/films')
.then(res => {
filmsArray = res.data.results
films = filmsArray
})
.then(res.render('task1/allMovies', {
pageTitle: "Movies",
films: films
}))
.catch(err => {
console.log(err)
})
}
so in ejs file I can extract id and title. I pass id to my route
<a href="/movie/<%=f.episode_id%>">
The problem is when I want to display character that appeared in the movie. For this I register a new route:
router.get('/movie/:movieId', movieController.getActors)
and in the controller :
exports.getActors = (req, res, next) => {
const movieId = req.params.movieId
let actorsData = []
let actors = []
axios.get('https://swapi.co/api/films/' + movieId)
.then(res =>{
actorsData = res.data.characters
})
.then( res => {
actorsData.map( actor => {
axios.get(actor)
.then(res => {
actors.push(res.data)
})
})
})
.then(res.render('task1/actors', {
pageTitle: 'Actors task 1',
actors: actors
})
)
.catch(err => {
console.log(err)
})
}
I am trying to push characters to actors array but it is constantly empty. Why? How to fix it?
At the end I would like to have a array of characters so I can pass it to my ejs file and display it over there.