admin管理员组

文章数量:1327062

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 :)

本文标签: