Home > other >  Uncaught (in promise) TypeError: fetch is not a function
Uncaught (in promise) TypeError: fetch is not a function

Time:09-24

class Fetch {
  async getCurrent(input) {
    const apiKey = "my_api_key";

    // make request to url

    const response = await fetch(
      `https:/api.openweathermap.org/data/2.5/weather?q=${input}&appid=${apiKey}`
    );

    const data = await response.json();
    console.log(data);
    return data;
  }
}

CodePudding user response:

I'm assuming you are running this code in Node.js, right?

If it's in the browser, you shouldn't have any issue since the fetch() API is implemented in the browser.

If you are running in Node.js you can use node-fetch which is a great implementation of fetch in Node.js.

CodePudding user response:

Your code works fine. Problem can be in that how you call function.

You need to create an object, outside the class and call the function.

let f = new Fetch();
f.getCurrent("Kiev");

You can`t use variable name fetch because this name is reserved.

  • Related