【发布时间】:2023-01-31 13:59:13
【问题描述】:
我将在我的 React 项目中使用 jsonwebtoken 创建表单数据标记。
import jwt from 'jsonwebtoken';
const MyForm = () => {
const submitForm = (e) => {
e.preventDefault();
const data = { name: 'name', email: 'email', subject: 'subject', message: 'message' };
const token = jwt.sign(data, 'qwerty');
console.log(token);
}
return(
<form onSubmit={submitForm}>
...
</form>
);
}
export default MyForm;
发生以下错误。
TypeError: Right-hand side of 'instanceof' is not an object
push../node_modules/jsonwebtoken/sign.js.module.exports [as sign]
../node_modules/jsonwebtoken/sign.js:108
105 | return failure(new Error('secretOrPrivateKey must have a value'));
106 | }
107 |
> 108 | if (secretOrPrivateKey != null && !(secretOrPrivateKey instanceof KeyObject)) {
| ^ 109 | try {
110 | secretOrPrivateKey = createPrivateKey(secretOrPrivateKey)
111 | } catch (_) {
View compiled
submitEmail
../MyForm.js:22
19 | e.preventDefault();
20 |
21 | const data = { name: 'name', email: 'email', subject: 'subject', message: 'message' };
> 22 | const token = jwt.sign(data, 'qwerty');
| ^ 23 | console.log(token)
24 |
25 |
为什么这会给我那个错误?
我试图解决这个问题,但还没有结果。
并感谢任何修复它的帮助。谢谢。
【问题讨论】:
-
只需转到您正在使用的 jsonwebtoken 包的 npm 页面,打开它的 github 存储库,打开问题选项卡并查找
'instanceof' is not an object。你会发现唯一的 1 issue 声明jsonwebtoken is a Node.js module, its use in a browser was never intended. Versions before 9.0.0 could use webpack's polyfill of the Node.js built-in modules.和一些额外的细节。 -
您使用的是什么版本的 Node 和 jsonwebtoken 模块?
标签: javascript reactjs jwt