Need help returning the response from an API?

Viewed 19

I want to return the response from the below API. What is wrong with this snippet. it receives the expected result but it doesn't return the response (it returns nothing).

export const findWeatherStats = async () => {
    try {
        let weatherStats: any;
        await request("https://api.openweathermap.org/data/2.5/weather?lat=6.9270786&lon=79.861243&appid=36758d4932b2b0dc7c3db0f0bc9816c9&units=metric",
            {json: true}, (err, res, body) => {
                if (err) {
                    return console.log(err);
                }
                weatherStats = body;
                console.log(weatherStats);
            });
        return weatherStats;

    } catch (e) {
        throw e;
    }
};
2 Answers

You should try weatherStats = res.body.

Related