最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to solve Unhandled rejection TypeError: Converting circular structure to JSON for jwt.sign - Stack Overflow

programmeradmin4浏览0评论

When I try to create a token for a user like in this code :

const jwt = require('jsonwebtoken');
const passport = require('passport');
const Patient = require('../models').Patient;

module.exports = {
    retrieve(req, res) {
        return Patient
            .find({
                where: {
                    email: req.body.email,
                }
            })
            .then(patient => {
                if (!patient) return res.json({
                    success: false,
                    msg: 'Patient not found'
                });
                const result = Patient.build().verifyPassword(req.body.password, patient.password);
                if (!result) {
                    return res.json({
                        success: false,
                        msg: 'wrong password'
                    });
                } else {
                    const token = jwt.sign(patient, secret, {
                        expiresIn: 604800 // 1 week
                    });
                    return res.status(201).send(patient);
                }
            })

    },
    //
};

I get this error :

Unhandled rejection TypeError: Converting circular structure to JSON at Object.stringify (native) at toString (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/tostring.js:9:15) at jwsSecuredInput (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:12:34) at Object.jwsSign [as sign] (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:22:22) at Object.module.exports [as sign] (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jsonwebtoken/sign.js:146:16) at Model.Patient.find.then.patient (/home/omarou/Documents/Projet/PharmaLiv/server/controllers/patients.js:27:39) at Model.tryCatcher (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:651:20) at tryOnImmediate (timers.js:624:5)

controllers/patients.js:27:39 refer to the method jwt.sign on my code

It tells that it's ing from the jwt.sign method

Anyone can tell me what's the matter ?

When I try to create a token for a user like in this code :

const jwt = require('jsonwebtoken');
const passport = require('passport');
const Patient = require('../models').Patient;

module.exports = {
    retrieve(req, res) {
        return Patient
            .find({
                where: {
                    email: req.body.email,
                }
            })
            .then(patient => {
                if (!patient) return res.json({
                    success: false,
                    msg: 'Patient not found'
                });
                const result = Patient.build().verifyPassword(req.body.password, patient.password);
                if (!result) {
                    return res.json({
                        success: false,
                        msg: 'wrong password'
                    });
                } else {
                    const token = jwt.sign(patient, secret, {
                        expiresIn: 604800 // 1 week
                    });
                    return res.status(201).send(patient);
                }
            })

    },
    //
};

I get this error :

Unhandled rejection TypeError: Converting circular structure to JSON at Object.stringify (native) at toString (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/tostring.js:9:15) at jwsSecuredInput (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:12:34) at Object.jwsSign [as sign] (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:22:22) at Object.module.exports [as sign] (/home/omarou/Documents/Projet/PharmaLiv/node_modules/jsonwebtoken/sign.js:146:16) at Model.Patient.find.then.patient (/home/omarou/Documents/Projet/PharmaLiv/server/controllers/patients.js:27:39) at Model.tryCatcher (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:651:20) at tryOnImmediate (timers.js:624:5)

controllers/patients.js:27:39 refer to the method jwt.sign on my code

It tells that it's ing from the jwt.sign method

Anyone can tell me what's the matter ?

Share Improve this question edited Mar 21, 2017 at 16:29 Omarou asked Mar 21, 2017 at 15:50 OmarouOmarou 471 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

"circular structure" means that something in the object you are trying to return contains a reference to itself or to a thing that it is contained in. This sort of structure can't easily be serialized.

It looks like the problem must be in the structure of your Patient object itself. You will need to simplify it for signing or sending over the wire

I found the mistake the object patient has too much methods that make the circular structure. so I created a variable playload that contains the variable that I need for authentication.

const payload = {email: patient.username, password: patient.password};
                    const token = jwt.sign(payload, secret, {
                        expiresIn: 604800 // 1 week
                    });

it works now thanks to @andrewMcGuinness for his answer :)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论