0

Hi I'm testing this JS function using OpenWeatherMap API, but when I fetch the request the promise jumps to .then() with pending status, but the API is answering with HTML code.

Here's my code:

function onSearch(city){
    fetch(`api.openweathermap.org/data/2.5/weather?q=London&appid=${ApiKey}&units=metric`)
      .then(response => response.json())
      .then(data => {
        console.log(data)
        if(data.main !== undefined){
          const city = {
            min: Math.round(data.main.temp_min),
            max: Math.round(data.main.temp_max),
            id: data.id,
            img: data.weather[0].icon,
            wind: data.wind.speed,
            temp: data.main.temp,
            name: data.name,
            logitude: data.coord.lon,
            latitude: data.coord.lat
          };
        }
      })
      .catch(e => console.log(e))
  }

Here's The API response

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="/manifest.json" />
    <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>Weather App</title>
  <script defer src="/static/js/bundle.js"></script></head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

And actually this is my index.html code

Any ideas on how to solve this issue?

  • 1
    Are you sure that you are receiving valid *JSON* data? `Unexpected token < ...` indicates that you are actually receiving HTML. You can verify this in the network tab of the developer tool. – Đinh Carabus Jan 21 '22 at 22:21
  • It's showing that error 'cause the promise is returning – Santiago Saavedra Jan 21 '22 at 22:25
  • `` is just the state of the promise (as presented in the console), but that is not the content that is parsed. You can inspect the actual result of the request in the network tab. – Đinh Carabus Jan 21 '22 at 22:33
  • But when I log the response it logs `````` – Santiago Saavedra Jan 21 '22 at 22:40
  • 1
    The promise was never resolved, but rejected because of an error. It's likely that you're receiving HTML or XML instead of JSON. Instead of `response.json()` change it (temporarily) to `response.text()` and see what it logs. – code Jan 21 '22 at 22:42
  • 1
    So you want to use the composed `city` object somewhere ..... ? – Roamer-1888 Jan 22 '22 at 01:30

1 Answers1

0

Checking in depth my code is was missing the "http://" at the begining of the URL when fetching