Pulse Nova
Pulse Nova

Reputation: 327

I am always getting circular structure to JSON error and unhandled promise rejection warning. How do I get rid of it?

As I am always getting unhandled promise rejection even though I have used the .catch(). How do I resolve it??

Here is my error message:

(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. 

Here's the code in 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);
    });

})

How do I resolve this error??

Any help is appreciated.

Upvotes: 0

Views: 458

Answers (1)

Ayush
Ayush

Reputation: 131

The problem seems to be res getting shadowed by the promise callback parameter in the then call on Payment.update(), instead use another variable.

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);
    });

})

Upvotes: 2

Related Questions