代码之家  ›  专栏  ›  技术社区  ›  kyrolos magdy

循环JSON,当尝试字符串化JS对象时

  •  0
  • kyrolos magdy  · 技术社区  · 4 年前

    我们收到这个错误:

    Converting circular structure to JSON
    >      --> starting at object with constructor 'ClientRequest'
    >      |     property 'socket' -> object with constructor 'TLSSocket'
    >      --- property '_httpMessage' closes the circle
    

      app.post('/payment-key-request', async (req: any, res: any) => {
      const {auth_token, order_id} = req.body;
      const payload1 = {
        auth_token,
        amount_cents: 100,
        expiration: 3600,
        order_id,
        billing_data: {
          apartment: 803,
          email: "claudette09@exa.com",
          floor: 42,
          first_name: "Clifford",
          street: "Ethan Land",
          building: 8028,
          phone_number: 9135210487,
          shipping_method: "PKG",
          postal_code: 41523,
          city: "Jaskolskiburgh",
          country: "CR",
          last_name: "Nicolas",
          state: "Utah"
        },
        currency: "EGP",
        integration_id: 1 // something goes off here
      };
    
    
        let cache: any = [];
    await Axios.post(
        "https://accept.paymobsolutions.com/api/acceptance/payment_keys",
        JSON.stringify(payload1),
        {
          headers: { "Content-Type": "application/json" }
        }
      )
        .then((resp: any) => {
          res.status(200).json({ ...resp });
        })
        .catch((error: any) => {
          if (error.response) {
            console.log("// Request made and server responded");
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
          } else if (error.request) {
            console.log("// The request was made but no response was received");
            console.log(error.request);
          } else {
            console.log(
              "// Something happened in setting up the request that triggered an Error"
            );
            console.log(error.message);
          }
          res.status(400).json({ ...error });
        });
    
        cache = null;
    })
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Ry-    4 年前

    response object ,您应该只发送已解析的正文:

    res.status(200).json(resp.data);
    

    res.status(400).json(error.response.data);