簡體   English   中英

不斷收到“非法 arguments:未定義,Object.bcrypt.hashSync 處的字符串”

[英]keeps getting "Illegal arguments: undefined, string at Object.bcrypt.hashSync"

我一直在我的 MERN 項目中使用 Bcrypt 我正在嘗試創建一個身份驗證系統我正在嘗試在 Postman 上運行測試,但我不確定為什么我不斷收到錯誤消息:“非法 arguments:未定義,字符串在 Object.bcrypt.hashSync"

這是我的 postman 請求: 在此處輸入圖像描述

這是 Controller 代碼:

const config = require("../config/auth.config");
const db = require("../models");
const User = db.user;
const Role = db.role;

var jwt = require("jsonwebtoken");
var bcrypt = require("bcryptjs");

exports.signup = (req, res) => {
 const user = new User({
  username: req.body.username,
  email: req.body.email,
  password: bcrypt.hashSync(req.body.password, 8),
});

user.save((err, user) => {
if (err) {
  res.status(500).send({ message: err });
  return;
}

if (req.body.roles) {
  Role.find(
    {
      name: { $in: req.body.roles },
    },
    (err, roles) => {
      if (err) {
        res.status(500).send({ message: err });
        return;
      }

      user.roles = roles.map((role) => role._id);
      user.save((err) => {
        if (err) {
          res.status(500).send({ message: err });
          return;
        }

        res.send({ message: "User was registered successfully!" });
      });
    }
  );
} else {
  Role.findOne({ name: "user" }, (err, role) => {
    if (err) {
      res.status(500).send({ message: err });
      return;
    }

    user.roles = [role._id];
    user.save((err) => {
      if (err) {
        res.status(500).send({ message: err });
        return;
      }

      res.send({ message: "User was registered successfully!" });
     });
   });
  }
 });
};

exports.signin = (req, res) => {
  User.findOne({
   username: req.body.username,
  })
.populate("roles", "-__v")
.exec((err, user) => {
  if (err) {
    res.status(500).send({ message: err });
    return;
  }

  if (!user) {
    return res.status(404).send({ message: "User Not found." });
  }

  var passwordIsValid = bcrypt.compareSync(
    req.body.password,
    user.password
  );

  if (!passwordIsValid) {
    return res.status(401).send({ message: "Invalid Password!" });
  }

  var token = jwt.sign({ id: user.id }, config.secret, {
    expiresIn: 86400, // 24 hours
  });

  var authorities = [];

  for (let i = 0; i < user.roles.length; i++) {
    authorities.push("ROLE_" + user.roles[i].name.toUpperCase());
  }

  req.session.token = token;

  res.status(200).send({
    id: user._id,
    username: user.username,
    email: user.email,
    roles: authorities,
  });
 });
};

exports.signout = async (req, res) => {
  try {
    req.session = null;
  return res.status(200).send({ message: "You've been signed out!" });
 } catch (err) {
 this.next(err);
 }
};

錯誤消息: Illegal arguments: undefined, string at Object.bcrypt.hashSync想說您將undefined作為參數傳遞給hashSync ZC1C425268E68385D14AB5074C17A4。 我們需要修復這個錯誤。

仔細查看發生錯誤的這一行:

password: bcrypt.hashSync(req.body.password, 8),

req.body.passwordundefined ,你可以通過console.log(req.body.password)來驗證。 問題是您將數據作為 URL 參數發送。 所以req.body是一個空的 object 並且req.body.passwordundefined

In Postman, select the Body tab, choose JSON format, then type your data as a JSON object. 然后,在您的代碼中,使用express.json()中間件以 JSON 格式解析請求。 您將擁有所需的 output。

您可以在下面的 Postman 中看到我的示例請求: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM