Reputation: 4313
I'm creating a microservice by calling another API inside this API. The other API returns the data but I keep getting this error
This is payment Response { success: true, json: 1 } (node:31709) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON at JSON.stringify () at stringify (/Users/greatness/microservice/order/node_modules/express/lib/response.js:1119:12) at ServerResponse.json (/Users/alpha/setel/order/node_modules/express/lib/response.js:260:14) at router.post (/Users/alpha/setel/order/src/routes/order.js:59:21) at at process._tickCallback (internal/process/next_tick.js:189:7) (node:31709) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
router.post("/order", async (req, res) => {
let paymentResponse;
// Im using Mongoose
const order = new Order()
try {
// Call the payment API
paymentResponse = await axios.post('http://localhost:3002/api/v1/payment', {
order
})
} catch (err) {
res.status(500).json({
success: false,
message: err.message
})
}
console.log("This is payment Response", paymentResponse.data)
// Success change the order status to confirmed
if (paymentResponse.data.json === 0) {
order.status = "confirmed"
} else {
order.status = "declined"
}
order.save()
res.status(200).json({
success: true,
paymentResponse,
order
})
})
The other just returning normal json
router.post("/v1/payment", async (req, res) => {
try {
// If 0 confirmed if 1 declined
const randomResponse = Math.round(Math.random())
res.status(200).json({
success: true,
json: randomResponse
})
} catch (err) {
res.status(500).json({
success: false,
message: err.message
})
}
})
What can I do? I keep getting status 500.
Regards.
Upvotes: 2
Views: 355
Reputation: 13830
You're calling json
with like this:
res.status(200).json({
success: true,
paymentResponse, <---
order
})
The paymentResponse
is a response object from axios, this is NOT a simple json, rather a complex JS objects with methods, properties and circular references.
What you want to do, is to send only the raw data like this:
res.status(200).json({
success: true,
paymentResponse: paymentResponse.data, <--- Make sure the response from payment is valid json!
order
})
Upvotes: 1