TypeError secret must be a string or bufferor a KeyObject--- node项目报错

TypeError: secret must be a string or bufferor a KeyObject — node项目报错

事情是这样的,我是准备发送post请求生成token

在提交post请求之后,终端报错

throw err; // Rethrow non-MySQL errors
      ^

TypeError: secret must be a string or bufferor a KeyObject
    at typeError (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\jwa\index.js:115:10)
    at checkIsSecretKey (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\jwa\index.js:82:11)
    at Object.sign (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\jwa\index.js:130:5)
    at Object.jwsSign [as sign] (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\jws\lib\sign-stream.js:32:24)
    at module.exports [as sign] (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\jsonwebtoken\sign.js:204:16)
    at Query.<anonymous> (F:\font-daun-study\node+vue_project\xd-class\server\controller\userController.js:95:27)
    at Query.<anonymous> (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\mysql\lib\Connection.js:526:10)
    at Query._callback (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\mysql\lib\Connection.js:488:16)
    at Sequence.end (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)   
    at Query._handleFinalResultPacket (F:\font-daun-study\node+vue_project\xd-class\server\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)

Node.js v18.6.0

可以理解为,某个参数格式传递错误

也怪我,粗心了,原来是引用导出文件时候忘记解构,导致将整个对象当成参数传入其中,出现报错

// config/jwtSecretKey.js
module.exports = {
  jwtSecretKey: 'xdclass.net',
};
// userController.js
const jwtSecretKey  = require("../config/jwtSecretKey");
const token = jwt.sign(user, jwtSecretKey, { expiresIn: "1h" });

出现报错

改为

// userController.js
const { jwtSecretKey }  = require("../config/jwtSecretKey");
const token = jwt.sign(user, jwtSecretKey, { expiresIn: "1h" });

成功执行

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值