0

I have a function where I get the weather for a certain location based on the long and lat of the location, using the imported npm package current-weather-data. I have never worked with promises before as I typically use await and async, but this seems interesting and good to learn... when running this chunk of code below, it doesn't render the page and gives me the error (node:52585) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:52585) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

this is my code:

 app.get('/admin-dashboard', (req, res) => {
    if (req.session.loggedin) {

     
      
        const location = {
            lat: 43.955540,
            lon: -79.480950
        }
         
    

        getWeather(location)
            .then(weather => {
                console.log(`The temperature here is: ${weather.temperature.value}`)
               
         
          
        res.render("admin-dashboard", {
            page_title: "admin-dashboard",
            FN: req.session.firstname,
            LN: req.session.lastname,
            weather: ${weather.temperature.value},
          });
        }).catch(err => console.log(err));
    } else {
        res.render('login.ejs')
    }
})
Gianluca
  • 900
  • 8
  • 27
  • This seems to have been answered here: [What is an unhandled promise rejection?](https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection). – rezso.dev Mar 12 '21 at 00:22
  • Does this answer your question? [What is an unhandled promise rejection?](https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection) – rezso.dev Mar 12 '21 at 00:31

1 Answers1

0

You are not catching errors thrown by the getWeather function. You can either add a .catch to the function, e.g.

app.get('/admin-dashboard', (req, res) => {
    if (req.session.loggedin) {
      const location = {
            lat: 43.955540,
            lon: -79.480950
        }
      getWeather(location).then(weather => {
        console.log(`The temperature here is: ${weather.temperature.value}`);
        res.render('admin-dashboard', {
          page_title: "admin-dashboard",
          FN: req.session.firstname,
          LN: req.session.lastname,
          weather: weather.temperature.value,
        }).catch(err => console.log(err));
      })
    } else {
      res.render('login.ejs')
    }
})

Or if you are more comfortable you could use async/await

try {
    const weather = await getWeather(location);
    console.log(`The temperature here is ${weather.temperature.value}`);

    res.render("admin-dashboard", {
        page_title: "admin-dashboard",
        FN: req.session.firstname,
        LN: req.session.lastname,
        weather: weather.temperature.value,
    });
} catch(err) {
    console.log(err);
}

Note i'm only logging errors, you can handle them as you see fit.

Wodlo
  • 847
  • 5
  • 8
  • I've edited my question to show what I did based on your helpful answer, but it failed... did I put the .catch in the wrong place? – Gianluca Mar 12 '21 at 00:40
  • Hmm strange, it looks fine to me. Are you still getting the `UnhandledPromiseRejectionWarning` or are you now getting the actual error that is being caught logged out? – Wodlo Mar 12 '21 at 10:45
  • yes I am still getting the same error, the `UnhandledPromiseRejectionWarning` one. – Gianluca Mar 12 '21 at 13:32
  • Ah I think you were missing some backticks when setting the value of the `weather` property of render object, i.e. it should be `\`${weather.temperature.value}\`` rather than just `${weather.temperature.value}` or in this case you can simply remove the curly brackets all together since you are not actually making use of the string interpolation. This was throwing off the formatting and therefore the catch was in the wrong place. I've updated my answer to reflect the fixed version – Wodlo Mar 13 '21 at 14:45