0

Good day, I'm trying to learn how to build a certain application but I need some help or at least some documentation about what I'm trying to do.

I already have my node.js/express.js ready to listen and serve what I can ask.

My next step which is where I struggle I need to call an API to pipe this API back into a response.

What I want to achieve:

I have a Google Sheet that calls an API already, but some time this API is down and I need to call a second one. Both have different structure so I'm trying to create my own API (Basically piping one of those 2 API) to pass it to my Google Sheet the way I want it.

What I've accomplish, I can call the API and pass the response to the console, but I haven't been able to find a way to pass this response back to a browser for example (res.send(response)) it normally gives me some asynchronous errors

import 'dotenv/config';
import cors from 'cors';
import express from 'express';
import axios from 'axios';

const app=express();

const https=require("https");

const url="https://jsonplaceholder.typicode.com/todos/1"

app.use(cors());

app.get('/', (req, res) => {
 axios.get(url)
 .then(function(response){
  res.send(response);
 })
 .catch(function(error){
  console.log(error);
 })
});

app.listen(process.env.PORT, () =>
 console.log('App listening on port '+process.env.PORT),
 );

TypeError: Converting circular structure to JSON

That's the error I keep getting.

I'm using Axios in that example but I have tried other solution too but I can't seem to find a way to do it.

I'm a beginner with all of this, I tried many search on google already but I don't know the proper wording for what I'm looking to do :-/

So quick recap

API1 or API2 return their response > I Manipulate their response > I send the response back to my own application

pSyToR
  • 873
  • 2
  • 7
  • 15
  • where is this error message showing up ? At the node service ? – trk Mar 04 '19 at 02:12
  • @82Tuskers It's being returned in the console TypeError: Converting circular structure to JSON at JSON.stringify () at stringify (C:\Users\slydi\Desktop\SWGOH GITHub\roadmap\node_modules\express\lib\response.js:1119:12) at ServerResponse.json (C:\Users\slydi\Desktop\SWGOH GITHub\roadmap\node_modules\express\lib\response.js:260:14) at ServerResponse.send (C:\Users\slydi\Desktop\SWGOH GITHub\roadmap\node_modules\express\lib\response.js:158:21) at send (C:\Users\slydi\Desktop\SWGOH GITHub\roadmap\src/index.js:17:7) – pSyToR Mar 04 '19 at 02:20
  • is this file included ? Is it possible to get a glimpse of it ? Its most likely that you might have a circular structure in it. With more details I can probably help you troubleshoot. – trk Mar 04 '19 at 02:23
  • Some additional information that might help: https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json – trk Mar 04 '19 at 02:26
  • @82Tuskers Any API I tried gives me this error... You can check at the link I posted it's a really simple API https://jsonplaceholder.typicode.com/todos/1 (Which is what I think you're asking ;-) ) – pSyToR Mar 04 '19 at 02:26
  • true ! there does not seem the issue I cited (in the link). Can you share your express version please ... – trk Mar 04 '19 at 02:32
  • Installed this morning 6.4.1 – pSyToR Mar 04 '19 at 02:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189366/discussion-between-82tuskers-and-psytor). – trk Mar 04 '19 at 02:35

1 Answers1

0

I was trying to pass the reply straight instead of making it a variable.

You have to repass it inside your script before sending it with something like that:

fetch('https://apiserver.com/api/etc/')
    .then(function (res) {
        var contentType = res.headers.get("content-type");
        if(contentType && contentType.includes("application/json")) {
            return res.json();
        }
        else {
            throw ("server did not reply properly");
        }
    })
    .then(json => res.send(json))
    .catch(function (err) {
        console.log(err);
    })
pSyToR
  • 873
  • 2
  • 7
  • 15