简体   繁体   English

由于 json 的循环结构,我不断获得状态 500

[英]I keep getting status 500 because of Circular structure of json

I'm creating a microservice by calling another API inside this API.我正在通过调用此 API 中的另一个 API 来创建微服务。 The other API returns the data but I keep getting this error另一个 API 返回数据,但我不断收到此错误

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.这是付款响应{成功:true,json:1}(节点:31709)UnhandledPromiseRejectionWarning:TypeError:将循环结构转换为JSON at JSON.stringify () at stringify (/Users/greatness/microservice/order/node_modules/express/lib /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) at process._tickCallback (internal/process/next_tick.js:189:7) (node:31709) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 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().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 2) (拒绝编号: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另一个只是返回正常的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.我一直收到状态 500。

Regards.问候。

You're calling json with like this:你是这样调用json

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. paymentResponse是来自 axios 的响应对象,这不是一个简单的 json,而是一个具有方法、属性和循环引用的复杂 JS 对象。 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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM