如何解决未处理的拒绝TypeError:为jwt.sign将循环结构转换为JSON

Oma*_*mer 2 javascript json request jwt

当我尝试创建一个令牌就像一个用户的代码:

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

    },
    //
};
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

未处理的拒绝TypeError:在atString(/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/tostring.js:9:15)的jwsSecuredInput(/ home /)将Object结构转换为JSON at Object.stringify(native) 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)在Model.Patient.find的Object.module.exports [as sign](/home/omarou/Documents/Projet/PharmaLiv/node_modules/jsonwebtoken/sign.js:146:16) .then.patient(/home/omarou/Documents/Projet/PharmaLiv/server/controllers/patients.js:27:39)在Model.tryCatcher(/ home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js /发布/ util.js:16:23)Promise._settlePromise(/ home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:569:18)at Pr Promise._settlePromises中的omise._settlePromise0(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:614:10)(/ home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird /js/release/promise.js:693:18)在Async._drainQueue(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:133:16)的Async._drainQueues( /home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:143:10)在Immediate.Async.drainQueues(/ home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js) /release/async.js:17:14)在tryOnImmediate的runCallback(timers.js:651:20)(timers.js:624:5)

controllers/patients.js:27:39 请参阅我的代码中的方法jwt.sign

它告诉它来自jwt.sign方法

谁能告诉我发生了什么事?

And*_*ess 5

"循环结构"意味着您尝试返回的对象中的某些内容包含对自身或其所包含的内容的引用.这种结构无法轻松序列化.

看起来问题必须在您的Patient对象本身的结构中.您需要简化它以进行签名或通过网络发送