2017-08-25 12 views
0

screenshot of errorを生成するときに、私はアプリケーションにJSONウェブトークン認証を実装していると私が言うエラーだ「JSONに円形構造を変換」:未処理の拒否はTypeError JSONウェブトークン

を「未処理の拒否はTypeError:円形の変換を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(「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; 
+0

エラースタックトレースを共有できますか? – Gaafar

+0

質問の先頭にあるエラースクリーンショットへのリンクを追加しました – OlatunjiYSO

答えて

0

(ペイロードを文字列化に失敗だと思われますオブジェクト)とにかくat this line in a dependency of a dependency

、それはおそらくsequelizeエンティティラッパーのだ、あなたはsequelizeモデルからプレーンなオブジェクトを取得するためのより多くの方法のため、この質問を確認することができ、この

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

のようにそれを呼び出してみてください。Sequelize, convert entity to plain object

+0

それを解決しました。ありがとうございました – OlatunjiYSO

+0

うれしかったです。解決したら回答を受け入れてください。 – Gaafar

関連する問題