screenshot of error我正在应用程序上实现json Web令牌身份验证,我收到错误消息:
"未处理的拒绝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("/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;
答案 0 :(得分: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