javascript - 由于 json 的循环结构,我不断获得状态 500
问题描述
我通过在这个 API 中调用另一个 API 来创建一个微服务。其他 API 返回数据,但我不断收到此错误
这是付款响应{成功:true,json:1}(节点:31709)UnhandledPromiseRejectionWarning:TypeError:在字符串化(/Users/greatness/microservice/order/node_modules/express/lib的JSON.stringify()中将循环结构转换为JSON /response.js:1119:12) 在 serverResponse.json (/Users/alpha/setel/order/node_modules/express/lib/response.js:260:14) 在 router.post (/Users/alpha/setel/order /src/routes/order.js:59:21) 在 process._tickCallback (internal/process/next_tick.js:189:7) (node:31709) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。(拒绝编号: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
})
})
另一个只是返回正常的 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
})
}
})
我能做些什么?我不断获得状态 500。
问候。
解决方案
你这样打电话json
:
res.status(200).json({
success: true,
paymentResponse, <---
order
})
这paymentResponse
是一个来自 axios 的响应对象,这不是一个简单的 json,而是一个具有方法、属性和循环引用的复杂 JS 对象。你想要做的是只发送这样的原始数据:
res.status(200).json({
success: true,
paymentResponse: paymentResponse.data, <--- Make sure the response from payment is valid json!
order
})
推荐阅读
- node.js - 本地主机拒绝连接快递
- javascript - 绑定下拉选择到输入框
- angular - 使用 ID 调用我的 Angular 应用程序入口点时出现问题 - 无法匹配任何路由
- github-actions - 分叉到我的组织后发现 github 操作时出错
- spring - Spring API 映射产生条件或条件
- apache-camel - Camel K 不从 FTP 下载文件
- javascript - 使用传单单击 onEachFeature
- typescript - 在 react-native 测试中模拟硬件回压
- java - 错误:无法访问 jarfile start.jar - 系统找不到指定的路径(加载 GeoServer 时)
- rundeck - Rundeck Windows 作业总是成功