【问题标题】:Unhandled rejection TypeError " converting circular structure to json " when generating json web token生成 json Web 令牌时出现未处理的拒绝 TypeError“将循环结构转换为 json”
【发布时间】:2018-02-03 19:18:27
【问题描述】:

screenshot of error 我正在对应用程序实施 json Web 令牌身份验证,但出现以下错误:

“未处理的拒绝类型错误:将循环结构转换为 JSON”

我进一步发现这个错误发生在执行到我的 webtoken 生成时,因为我注释掉它时没有发生错误。

请在下面找到我的代码:

    const express= require("express");
    const router= express.Router();
    let Users = require("../models/users");
    const jwt= require("jsonwebtoken");
    const configuration = require("../config");
    const app = express();
    app.set("superSecret", configuration.secret);

    //registered user submitting signin form
    router.post("/users/signin", function(req, res, next){
      let confirm;
      Users.findOne({
        where:{ username: req.body.username}
      }).then(user => {
        if(!user){
          res.send("No such users found!")
        }
        else if(user){
           confirmed = user.password === req.body.password;
          if(confirmed){
            let token = jwt.sign(user, app.get("superSecret"), 
                {expiresIn: 1440}); 
                    //expiresInMinutes
                res.json({
                            success: true,
                            message: "enjoy your json",
                            token: token
                          })
          }
          else{
            res.send('incorrect password');
          }
        }
      })
    });

如果我注释掉 let token = jwt.sign(user, app.get("superSecret).. 就不会有错误。 感谢期待。

在我的用户模型下面找到。

    const Sequelize= require('sequelize')
    const bcrypt = require('bcrypt-nodejs')
    const sequelStorage = new Sequelize('newtrial', 'olatunji', '5432', {
      host: 'localhost',
      dialect: 'postgres',

        pool: {
        max: 5,
        min: 0,
        idle: 10000
      },

    });


    let Users = sequelStorage.define('users', {
      username:{
        type: Sequelize.STRING,
        allowNull: false
      },
      email: {
        type: Sequelize.STRING,
        allowNull: false,
        unique: true,
        validate: { isEmail: true}
      },
      password:{
        type: Sequelize.STRING,
        allowNull:false
      },
      admin:{
        type: Sequelize.BOOLEAN,
        allowNull: false,
        default: false
      }
    })

    sequelStorage.sync()
   [enter image description here][1] .catch(function(error){
      console.log(error);
    });

    module.exports= Users;

【问题讨论】:

  • 你能分享错误堆栈跟踪吗?
  • 我在问题顶部添加了指向错误截图的链接

标签: javascript json jwt


【解决方案1】:

似乎无法对有效负载(user 对象)at this line in a dependency of a dependency 进行字符串化

反正可能是sequelize entity wrapper的原因,试试这样调用吧

let token = jwt.sign(user.toJSON(), app.get("superSecret"), {expiresIn: 1440});

您可以查看此问题以了解更多从 sequelize 模型中获取普通对象的方法:Sequelize, convert entity to plain object

【讨论】:

  • 很高兴它成功了。如果解决了,请采纳答案。
猜你喜欢
  • 2021-11-03
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 2018-10-05
  • 2016-01-22
  • 2019-01-05
相关资源
最近更新 更多