Home > Blockchain >  Need help returning the response from an API?
Need help returning the response from an API?

Time:09-06

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;
    }
};

CodePudding user response:

If I’m not wrong request is deprecated. You can use native fetch API or axios library.

Fetch - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API Axios - https://axios-http.com/docs/intro

CodePudding user response:

You should try "weatherStats = res.body"

  • Related