2017-08-25 30 views
0

screenshot of error我上的應用程序執行一個JSON網絡令牌認證和我得到它說錯誤:未處理排斥類型錯誤「轉換圓形結構爲JSON」生成JSON網絡令牌時

「未處理的排斥類型錯誤:轉換圓形結構到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("https://stackoverflow.com/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'); 
      } 
     } 
     }) 
    }); 

如果我註釋掉讓令牌= jwt.sign(用戶,app.get(「絕密)..就沒有錯誤 感謝。在

期待。在下面找到我的用戶模型。

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; 
+0

你能分享錯誤堆棧跟蹤嗎? – Gaafar

+0

我已經添加了一個鏈接到問題頂部的錯誤截圖 – OlatunjiYSO

回答

0

看來,它的失敗到字符串化的有效載荷(user對象)at this line in a dependency of a dependency

無論如何,這可能是因爲sequelize實體包裝的,嘗試調用它像這樣

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

您可以檢查這個問題更多的方式來從sequelize模型得到的純對象:Sequelize, convert entity to plain object

+0

解決了它,它現在工作正常!謝謝 – OlatunjiYSO

+0

很高興它的工作。如果解決了,請接受答案。 – Gaafar

相關問題