1

I have build a server with express.js and part of it looks like this:

app.get("/api/stuff", (req, res) => {
  axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
    res.send(response);
    console.log('response=',response);
  })
});

When I hit 'api/stuff' it returns an error:

(node:1626) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Converting circular structure to JSON

How can I return the json from my endpoint?

bier hier
  • 20,970
  • 42
  • 97
  • 166

1 Answers1

2

The response object you get from the open weather API is circular type(objects which reference themselves). JSON.stringify will throw an error when it comes through a circular reference. This is the reason you are getting this error while using send method.

to avoid this just send the required data as response

app.get("/api/stuff", (req, res) => {
  axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){
    res.send(response.data);
    console.log('response=',response.data);
  })
});
sbr
  • 644
  • 8
  • 19