javascriptnode.jsisomorphic-fetch-api

Why is "Promise { <pending> }" not resolving?


I am trying to fetch data from the Storm Glass API. I am using their template Fetch request (https://docs.stormglass.io/?javascript#point-request).

When I run the script the console reads out "Promise { <pending> }" indefinitely. So, the request is not returning a value but I can't work out why. Any ideas?

I have replaced my API key with <My API key>

const http = require('http')
const fetch = require('isomorphic-fetch');

http.createServer((req, res) => {

////////////////////////////////////////////////App code
const lat = 58.7984;
const lng = 17.8081;
const params = 'waveHeight,airTemperature';

fetch(`https://api.stormglass.io/point?lat=${lat}&lng=${lng}&params=${params}`, {
  headers: {
    'Authorization': '<My API key>'
  }
}).then(function(response) {
  // Do something with response data.
  const jsonData = response.json();
  console.log(jsonData)
});

/////////////////////////////////////////////////////////
}).listen(3000);

console.log("service running on http://localhost:3000");

Solution

  • The response.json function return a Promise, not the deserialized object. Your code should read:

    fetch(`https://api.stormglass.io/point?lat=${lat}&lng=${lng}&params=${params}`, {
      headers: {
        'Authorization': '<My API key>'
      }
    })
    .then(response => response.json())
    .then(function(jsonData) {
      // Do something with response data
      console.log(jsonData)
    });