关键词:
【中文标题】循环引用阻止 jsonwebtoken.sign JSON.stringify 的原因【英文标题】:Circular reference blocks jsonwebtoken.sign cause of JSON.stringify 【发布时间】:2017-03-20 03:28:06 【问题描述】:我正在使用 Express 和 Sequelize 进行基于 upon this tutorial 的基本用户身份验证
当我想向用户签署令牌时,我收到一条错误消息,告诉我我正在尝试 JSON.stringify()
一个无法完成的循环引用。因此引发错误,我无法将令牌分配给用户。
或当我在数据库中找到我的用户时,我做错了一个循环引用或我只需要找到一个解决方案来打破我想的循环引用.哪位大神能解释一下是哪两个?
完整的错误是:
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at toString (/Users/Desktop/express-jwt/node_modules/jws/lib/tostring.js:9:15)
at jwsSecuredInput (/Users/Desktop/express-jwt/node_modules/jws/lib/sign-stream.js:12:34)
at Object.jwsSign [as sign] (/Users/Desktop/express-jwt/node_modules/jws/lib/sign-stream.js:22:22)
at Object.module.exports [as sign] (/Users/Desktop/express-jwt/node_modules/jsonwebtoken/sign.js:144:16)
at Model.User.findOne.then.user (/Users/Desktop/express-jwt/server/index.js:69:27)
at Model.tryCatcher (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/async.js:138:16)
at Async._drainQueues (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/async.js:148:10)
at Immediate.Async.drainQueues (/Users/Desktop/express-jwt/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:574:20)
at tryOnImmediate (timers.js:554:5)
我的服务器索引是:
const express = require(`express`);
const app = express();
const bodyParser = require(`body-parser`);
const morgan = require('morgan');
const jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
const config = require('./config'); // get our config file
const db = require(`./models`);
const User = global.db.User;
const port = process.env.PORT || 8080;
db.sequelize.sync().then(() =>
console.log(`Express server listening on port $port`);
);
app.set('superSecret', config.secret);
app.use(bodyParser.urlencoded(extended: false));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.get('/', (req, res) =>
res.send('Hello! The API is at http://localhost:' + port + '/api');
);
app.listen(port);
console.log('Magic happens at http://localhost:' + port);
app.get('/setup', (req, res) =>
db.sequelize.sync().then(() =>
return User.create(
username: 'Kevin frafster',
password: 'password',
admin: true
)
.then(addedUser =>
console.log(addedUser.get(
plain: true
));
)
.catch(err =>
res.json(err);
);
);
);
// API ROUTES -------------------
// get an instance of the router for api routes
const apiRoutes = express.Router();
apiRoutes.post('/authenticate', (req,res) =>
User.findOne(
where: username: req.body.username
).then(user =>
if (!user)
res.json( success: false, message: 'Authentication failed. User not found.');
else
// console.log(user);
if (user.password != req.body.password)
res.json( success: false, message: 'Authentication failed. Wrong password.' )
else
const token = jwt.sign(user, app.get('superSecret'),
expiresIn: 60*60*24
);
res.json(
succes: true,
message: 'Enjoy your token!',
token
);
).catch(err =>
res.status(500).json(err);
)
);
// TODO: route to authenticate a user (POST http://localhost:8080/api/authenticate)
// TODO: route middleware to verify a token
// route to show a random message (GET http://localhost:8080/api/)
apiRoutes.get('/', (req, res) =>
res.json( message: 'Welcome to the coolest API on earth!' );
);
// route to return all users (GET http://localhost:8080/api/users)
apiRoutes.get('/users', (req, res) =>
User.findAll()
.then(users =>
res.json(users);
)
.catch(err =>
res.json(err);
);
);
// apply the routes to our application with the prefix /api
app.use('/api', apiRoutes);
【问题讨论】:
【参考方案1】:嗯,答案完全是花生。
1) 创建新对象并为其分配有效负载
const payload = username: user.username, password: user.password;
2) 使用新对象将令牌分配给
const token = jwt.sign(payload, app.get('superSecret'),
expiresIn: 60*60*24
);
【讨论】:
我不明白你的意思。哪个用户对象?你能解释一下你自己的解决方案吗?我遇到了同样的问题 谢谢。这拯救了我的一天TypeError:回调不是函数,“node_modules\jsonwebtoken\sign.js:101:14)”
】TypeError:回调不是函数,“node_modules\\\\jsonwebtoken\\\\sign.js:101:14)”【英文标题】:TypeError:callbackisnotafunction,"node_modules\\jsonwebtoken\\sign.js:101:14)"TypeError:回调不是函数,“node_modules\\jsonwebtoken\\sign.js:101: 查看详情
使用 Apple 新的 Combine 框架时如何防止强引用循环(.assign 导致问题)
】使用Apple新的Combine框架时如何防止强引用循环(.assign导致问题)【英文标题】:HowtopreventstrongreferencecycleswhenusingApple\'snewCombineframework(.assigniscausingproblems)【发布时间】:2020-01-1816:27:15【问题描述】:我不太明白如何在类中正确... 查看详情
阻止应用程序关闭
...因为没有任何东西可以保持线程打开/运行。添加while(true)循环使其保持打开状态,但冻结主线程。将 查看详情
如何阻止我的脚本循环
】如何阻止我的脚本循环【英文标题】:HowdoIstopmyscriptfromlooping【发布时间】:2017-06-0719:50:53【问题描述】:我正在针对我的AD运行附加的脚本。我只需要脚本返回每个AD帐户一次,但它似乎在无限循环中运行。我该如何阻止这种... 查看详情
为啥 Sleep() 函数会阻止整个循环工作?
】为啥Sleep()函数会阻止整个循环工作?【英文标题】:WhydoestheSleep()functionpreventtheentireloopfromworking?为什么Sleep()函数会阻止整个循环工作?【发布时间】:2021-08-1214:43:29【问题描述】:我们的目标是创建一个记录击键并将其写入... 查看详情
PHP循环阻止其余代码执行[关闭]
】PHP循环阻止其余代码执行[关闭]【英文标题】:PHPloopblockingtherestofthecodetoexecute[closed]【发布时间】:2012-11-0903:08:58【问题描述】:我有一个看起来有点像这样的循环的scipt[CODE][BIGLOOP][SOMEMORECODE]我面临的问题是,只要循环运行,... 查看详情
如何阻止我的动画按钮不断循环?
】如何阻止我的动画按钮不断循环?【英文标题】:Howtostopmyanimatedbuttonfromloopingconstantly?【发布时间】:2015-08-2609:53:34【问题描述】:我刚开始尝试在我的IOS开发中使用动画,当我点击一个按钮时我已经设法让动画工作了-但是它... 查看详情
如何阻止循环在 Java 中运行
】如何阻止循环在Java中运行【英文标题】:HowdoyoustopaloopfromrunninginJava【发布时间】:2011-06-2718:39:25【问题描述】:如何停止条件循环的运行。例如,如果我编写了一个接受0到100值的if语句。如果用户输入的数字小于0或大于100,... 查看详情
Qt 控制台应用程序:while 循环阻止事件循环
】Qt控制台应用程序:while循环阻止事件循环【英文标题】:QtConsoleApplication:whileloopblockseventloop【发布时间】:2011-08-0513:07:21【问题描述】:我正在制作一个控制台应用程序,我在其中获取用户输入使用std::getline()(在while循环中... 查看详情
如何阻止指针更新前一个循环中的数据?
】如何阻止指针更新前一个循环中的数据?【英文标题】:Howtostopapointerfromupdatingdatafromapreviousloop?【发布时间】:2017-11-2117:21:04【问题描述】:我正在使用for循环进行循环并将数据插入到NSMutableDictionary中,然后将该NSMutableDictiona... 查看详情
PHP:为啥用括号括起来的函数调用会阻止“通过引用”通知? [复制]
】PHP:为啥用括号括起来的函数调用会阻止“通过引用”通知?[复制]【英文标题】:PHP:Whydosurroundingfunctioncallswithparenthesesprevent\'passbyreference\'notices?[duplicate]PHP:为什么用括号括起来的函数调用会阻止“通过引用”通知?[复制]... 查看详情
循环阻止 DOM 在其之前操作代码
】循环阻止DOM在其之前操作代码【英文标题】:LoopblocksDOMmanipulatingcodebeforeit【发布时间】:2019-05-2921:59:38【问题描述】:这里我使用循环代码(借鉴What\'stheequivalentofJava\'sThread.sleep()inJavaScript?)来模拟网络延迟来检查我的加载动... 查看详情
如何阻止音乐曲目在 Libgdx 中循环播放
】如何阻止音乐曲目在Libgdx中循环播放【英文标题】:HowdoIstopamusictrackfromloopinginLibgdx【发布时间】:2020-04-1421:56:28【问题描述】:在我的项目中,我尝试在屏幕切换到游戏结束时播放音乐曲目。它在它应该开始的时候开始,但... 查看详情
java示例代码_无限循环无法找到阻止它的方法
java示例代码_无限循环无法找到阻止它的方法 查看详情
while循环阻止其他代码执行c#
】while循环阻止其他代码执行c#【英文标题】:whilelooppreventsothercodefromexecutingc#【发布时间】:2019-08-0117:43:52【问题描述】:我正在尝试编写一个绘图库。在这个绘图库中,有一个更新函数应该更新每一帧。我通过使用dowhileloop来... 查看详情
阻止 root.mainloop() 执行的无限循环 - Python
】阻止root.mainloop()执行的无限循环-Python【英文标题】:Infinitelooppreventingroot.mainloop()fromexecuting-Python【发布时间】:2022-01-2403:59:41【问题描述】:嘿,伙计们,这可能很愚蠢,但我无法找到解决方法...我试图每天运行我的python代... 查看详情
无法阻止python在循环中遍历字符串
】无法阻止python在循环中遍历字符串【英文标题】:Can\'tstoppythonfromiteratingthroughstringinloop【发布时间】:2021-12-1423:18:15【问题描述】:classHat:def__init__(self,**kwargs):self.contents=[]forballtypeinkwargs.keys():forballnuminrange(kwargs[balltype] 查看详情
有没有办法阻止 Shiny 引用 Bootstrap 3 共享文件夹?
】有没有办法阻止Shiny引用Bootstrap3共享文件夹?【英文标题】:IsthereawaytopreventShinyfromreferencingBootstrap3sharedfolder?【发布时间】:2021-12-2722:05:12【问题描述】:我在用sass编译后尝试使用我自己的引导样式表。使用ui<-fluidPage(theme=... 查看详情