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 ?
The "Converting circular structure to JSON" error occurs when we pass an object that contains circular references to the JSON. stringify() method. To solve the error, make sure to remove any circular references before converting the object to JSON.
A circular structure is an object that references itself. To be able to stringify such objects, developers can utilize the replacer parameter in the stringify() method, making sure the function that is being passed in, filters out repeated or circular data.
By default, Axios converts Javascript data to JSON (including AJAX). The “content-type” header is also set to “application/json.” If you send a serialized JSON object as data, however, Axios considers it as “application/x-www-form-urlencoded” (form-encoded request body).
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
})
});
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
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With