如何解决未处理的拒绝TypeError:为jwt.sign

时间:2017-03-21 15:50:41

标签: 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);
                }
            })

    },
    //
};

我收到此错误:

  

未处理拒绝TypeError:将循环结构转换为JSON       at Object.stringify(native)       在toString(/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/tostring.js:9:15)       在jwsSecuredInput(/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:12:34)       在Object.jwsSign [作为标志](/home/omarou/Documents/Projet/PharmaLiv/node_modules/jws/lib/sign-stream.js:22:22)       在Object.module.exports [作为标志](/home/omarou/Documents/Projet/PharmaLiv/node_modules/jsonwebtoken/sign.js:146:16)       在Model.Patient.find.then.patient(/home/omarou/Documents/Projet/PharmaLiv/server/controllers/patients.js:27:39)       在Model.tryCatcher(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/util.js:16:23)       在Promise._settlePromiseFromHandler(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:512:31)       在Promise._settlePromise(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:569:18)       在Promise._settlePromise0(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/promise.js:614:10)       在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)       在Immediate.Async.drainQueues(/home/omarou/Documents/Projet/PharmaLiv/node_modules/bluebird/js/release/async.js:17:14)       在runCallback(timers.js:651:20)       在tryOnImmediate(timers.js:624:5)

controllers/patients.js:27:39参考我的代码上的方法jwt.sign

它告诉它来自jwt.sign方法

任何人都可以告诉我这是怎么回事?

2 个答案:

答案 0 :(得分:5)

"圆形结构"意味着你想要返回的对象中的某些东西包含对它自己或它所包含的东西的引用。这种结构不容易被序列化。

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

答案 1 :(得分:1)

我发现了错误 对象患者有太多的方法来制作圆形结构。 所以我创建了一个变量playload,其中包含我需要进行身份验证的变量。

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

现在感谢@andrewMcGuinness的答案:)