首页 > 解决方案 > 我总是得到 JSON 错误和未处理的承诺拒绝警告的循环结构。我该如何摆脱它?

问题描述

因为即使我使用了 .catch(),我也总是收到未处理的承诺拒绝。我该如何解决?

这是我的错误信息:

(node:1420) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at stringify (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:158:21)
    at axios.post.then.catch.error (D:\Projects\classroom-webstack\backend\routes\Payment.js:93:13)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:1420) 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)
(node:1420) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 
    at stringify (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:260:14)
    at ServerResponse.send (D:\Projects\classroom-webstack\backend\node_modules\express\lib\response.js:158:21)
    at axios.post.then.catch.error (D:\Projects\classroom-webstack\backend\routes\Payment.js:93:13)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:1420) 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)
(node:1420) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

这是nodejs中的代码

// Payment Route
payments.post('/verify', (req, res) => {
    let data = {
        token: req.body.token,
        amount: req.body.amount
    }

    let config = {
        headers: {'Authorization': 'Key test_secret_key'}
    };

    axios.post("https://khalti.com/api/v2/payment/verify/", data, config)
    .then(response => {
        console.log("it works")
        Payment.update({status: true}, {
            where: {
                requestID :  req.body.request_id
            }
        })
        .then(res =>{
            res.status(200).json({status: "Ok"})
        })
        .catch(err =>{
            console.log(err.response)
        })
    })
    .catch(error => {
        console.log("it dont work")
        res.send(error.response);
    });

})

我该如何解决这个错误?

任何帮助表示赞赏。

标签: javascriptnode.jspromise

解决方案


问题似乎是resthen被调用中的 promise 回调参数所掩盖Payment.update(),而不是使用另一个变量。

payments.post('/verify', (req, res) => {
    let data = {
        token: req.body.token,
        amount: req.body.amount
    }

    let config = {
        headers: {'Authorization': 'Key test_secret_key'}
    };

    axios.post("https://khalti.com/api/v2/payment/verify/", data, config)
    .then(response => {
        console.log("it works")
        Payment.update({status: true}, {
            where: {
                requestID :  req.body.request_id
            }
        })
        .then(done =>{
    //        ^^^^
            res.status(200).json({status: "Ok"})
        })
        .catch(err =>{
            console.log(err.response)
        })
    })
    .catch(error => {
        console.log("it dont work")
        res.send(error.response);
    });

})

推荐阅读