39

My code is as shown below:

axios.post('https://api.sandbox.xyz.com/v1/order/new', JSON.stringify({
            "request": "/v1/order/new",
            "nonce": 123462,

            "client_order_id": "20150102-4738721",
            "symbol": "btcusd",
            "amount": "1.01",
            "price": "11.13",
            "side": "buy",
            "type": "exchange limit"
        }), config)
        .then(function(response) {
            console.log(response);
            res.json({
                data: JSON.stringify(response)
            })
        })
        .catch(function(error) {
            console.log(error);
            res.send({
                status: '500',
                message: error
            })
        });

Now it is saying that Unhandled promise rejection (rejection id: 2): TypeError: Converting circular structure to JSON for the code res.json({data:JSON.stringify(response)})

So, is there anything missing in this code ?

Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • 4
    Maybe you mean `JSON.stringify(response.data)`? The `response` itself presumably contains circular references (probably in `response.request`). – user94559 Jul 26 '17 at 06:33
  • yes even if I write `res.json({data:response})` , the error remains same – Mrugesh Jul 26 '17 at 06:38
  • Did you try what I suggested? – user94559 Jul 26 '17 at 06:48
  • Yes , I put ` console.log("response is " + response.request)`, but it is saying `response is [object object]` – Mrugesh Jul 26 '17 at 06:56
  • What I suggested is `res.json({ data: JSON.stringify(response.data) });` – user94559 Jul 26 '17 at 07:01
  • This is the solution for that: https://stackoverflow.com/a/11616993/7445344 I know its not the prettiest – LakiGeri Jul 26 '17 at 08:40
  • 1
    @LakiGeri That's only necessary if you're truly trying to serialize something with circular references. Here, it's almost certainly just a mistake that the thing being serialized has a circular reference. (This code is trying to serialize a `response` object when that's very likely not the intention.) – user94559 Jul 26 '17 at 18:13

6 Answers6

36
axios.post('https://api.sandbox.xyz.com/v1/order/new', JSON.stringify({
            "request": "/v1/order/new",
            "nonce": 123462,
            "client_order_id": "20150102-4738721",
            "symbol": "btcusd",
            "amount": "1.01",
            "price": "11.13",
            "side": "buy",
            "type": "exchange limit"
        }), config)
        .then(function(response) {
            res.send(response.data)
        })
        .catch(function(error) {
            res.send({
                status: '500',
                message: error
            })
        });
thomasmeadows
  • 1,835
  • 12
  • 15
12

This happens many a time with axios because sometimes we directly return the response from the endpoint. For example, this error will occur if we pass the response directly, rather than passing the response.data.

response = await axios.get("https://jsonplaceholder.typicode.com/users");
res.send(response); // error
res.send(response.data); // works perfectly
zalog
  • 683
  • 9
  • 21
Surya
  • 554
  • 5
  • 10
9

The problem might be because of the response you are sending out to the client is not a JSON object. In my case, I solved the error by simply sending the JSON part of the response object.

res.status(200).json({
  success:true,
  result:result.data
})
glinda93
  • 7,659
  • 5
  • 40
  • 78
Satya
  • 1,569
  • 8
  • 13
8

This worked for me.

res.status(200).json({
   data: JSON.parse(JSON.stringify(response.data)
}));
Romanas
  • 547
  • 7
  • 25
Ken Mbogo
  • 181
  • 2
  • 3
5
res.json({ data: JSON.stringify(response.data) });

This worked for me.

Shadab
  • 321
  • 5
  • 15
4

Try to add error handler interceptor:

const handle_axios_error = function(err) {

    if (err.response) {
        const custom_error = new Error(err.response.statusText || 'Internal server error');
        custom_error.status = err.response.status || 500;
        custom_error.description = err.response.data ? err.response.data.message : null;
        throw custom_error;
    }
    throw new Error(err);

}

axios.interceptors.response.use(r => r, handle_axios_error);
axios.post(....)

Thanks Sepehr Vakili for his post https://github.com/axios/axios/issues/836#issuecomment-390342342

Minh Nguyen
  • 1,989
  • 3
  • 17
  • 30
  • This really help me, before this I was dumping the request or response object and it would be huge (certificates, circular structure etc). Now I get exactly what I need to debug – Birla Jul 16 '21 at 08:05