【问题标题】:function _readOnlyError(name)函数_readOnlyError(名称)
【发布时间】:2020-08-22 02:45:48
【问题描述】:

我正在尝试对 JWT 进行一些身份验证,但出现此错误:

api\utils\index.js:16
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }

我不明白为什么。

import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import { config } from 'dotenv';

config();

export const jwtToken = {
  createToken({ id, email }) {
    return jwt.sign(
      { id, email },
      process.env.JWT_SECRET,
      { expiresIn: '24h' }
    );
  },
  verifyToken(token) {
    const decoded = jwt.verify(token, process.env.JWT_SECRET, { expiresIn: '24h' });
    return decoded;
  }
};


export const hashPassword = (hashPassword) = (password) => bcrypt.hashSync(password, 10);
export const comparePassword = (password, hash) => bcrypt.compareSync(password, hash);

【问题讨论】:

  • 您的错误的stack 是什么?它应该指出导致问题的原始代码行。
  • 错误:“hashPassword”是只读的。我用 const hashPassword 评论该行,它的工作我会处理它。谢谢。
  • 哦,我看到你的错字了。您有 export const hashPassword = (hashPassword) = (password) => bcrypt.hashSync(password, 10); 正在对 hashPassword 进行两次分配。你可能只想要export const hashPassword = (password) => bcrypt.hashSync(password, 10);
  • 是的,谢谢!

标签: javascript node.js jwt babeljs eslint


【解决方案1】:

相关问题,举个例子:

代码:

let obj1 = { Key: "key1" };

const { Key } = obj1;               // NOTE - Key is constant (`const`)

if (!Key) {
    Key = "key2";                   // NOTE - Key is constant (`const`) but overwritten
}

错误:

{"stack":"Error: \"Key\" is read-only\n    at _readOnlyError ... }

解决方案:

let { Key, Other } = obj1;          // NOTE - Now Key is  not constant (`let`)

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多