Я всегда получаю круговую структуру с ошибкой JSON и предупреждением об отказе от необработанного обещания. Как мне от этого избавиться? - PullRequest
0 голосов
/ 07 марта 2020

Поскольку я всегда получаю необработанное отклонение обещания, даже если я использовал .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);
    });

})

Как мне решить эту ошибку ??

Любая помощь приветствуется.

1 Ответ

2 голосов
/ 08 марта 2020

Кажется, проблема в том, что res затеняется параметром обратного вызова обещания в вызове then на 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);
    });

})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...