1
votes

I'm getting this error massage when I run my server.js file. Error Message:

let films = await response.data.films.map(film =>{ ^^^^^ SyntaxError: await is only valid in async function at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Module._compile (internal/modules/cjs/loader.js:657:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Function.Module.runMain (internal/modules/cjs/loader.js:742:12) at startup (internal/bootstrap/node.js:283:19

    app.get('/api/popular/movies', async (req, res, next)=> {

    axiosInstance.get(`filmsNowShowing/`).then(response=> {
        console.log(response.data.films)
        let films = await response.data.films.map(film =>{
            return {
                id:film.film_id,
                name:film.film_name,
                textLong:film.synopsis_long,
                picimage:film.images.poster
            }
        })
    res.status(200).json(films);
  }).catch(err => {
      console.log(err);
  });
});
1

1 Answers

0
votes

You missed an async inside the .then(response => { ... })

    app.get('/api/popular/movies', async (req, res, next)=> {

    await axiosInstance.get(`filmsNowShowing/`).then(async response => {
        console.log(response.data.films)
        let films = await response.data.films.map(film =>{
            return {
                id:film.film_id,
                name:film.film_name,
                textLong:film.synopsis_long,
                picimage:film.images.poster
            }
        })
    res.status(200).json(films);
  }).catch(err => {
      console.log(err);
  });
});