【问题标题】:How to solve Unhandled rejection TypeError: Converting circular structure to JSON for jwt.sign如何解决未处理的拒绝 TypeError: Converting circular structure to JSON for jwt.sign
【发布时间】:2017-08-13 09:46:33
【问题描述】:

当我尝试为 此代码 中的用户创建令牌时:

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

    },
    //
};

我收到此错误:

未处理的拒绝类型错误:将循环结构转换为 JSON 在 Object.stringify (本机) 在 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) 在 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) 在 runCallback (timers.js:651:20) 在 tryOnImmediate (timers.js:624:5)

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

它表明它来自 jwt.sign 方法

谁能告诉我怎么回事?

【问题讨论】:

    标签: javascript json request jwt


    【解决方案1】:

    “循环结构”意味着您尝试返回的对象中的某些内容包含对自身或包含它的事物的引用。这种结构不容易序列化。

    看起来问题一定出在您的 Patient 对象本身的结构上。您需要简化它以进行签名或通过电汇发送

    【讨论】:

    • 我的结构是这样的id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, email: { type: Sequelize.STRING, allowNull: false }, password : { type: Sequelize.STRING, allowNull: false }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } });你建议我改变什么?谢谢:)
    • 例如,当我向患者提出请求时,我的对象包含以下内容:` "patient": { "id": 7, "email": "dupont@gmail.com", "password" :“$2a$08$wqgTg8.DSPT.BJHM.BQV/eq5oQBXwGnxH49GlP1A2YMlGRsmYKNy。”,“createdAt”:“2017-03-21T15:34:40.977Z”,“updatedAt”:“2017-03-21T15:34:40.977Z " } `我没有看到任何圆形结构
    • 看起来不错。你确定错误是在 jwt.sign() 中生成的吗?
    • 请检查我在初始帖子中编辑的错误,我认为是jwt.sign因为controllers/patients.js:27:39参考这个方法
    • 如果您查看 node_modules/jws/lib/sign-stream.js:12:34,您可能会看到它在遇到错误时调用 toString 的内容
    【解决方案2】:

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

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

    感谢@andrewMcGuinness 的回答,它现在可以工作了 :)

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2022-01-08
      • 2023-01-18
      • 2019-04-03
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      相关资源
      最近更新 更多