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" });
成功执行