簡體   English   中英

非法 arguments:未定義,字符串

[英]Illegal arguments: undefined, string

注冊用戶時出現此錯誤:

(node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_jsports/bcryptjs/dist/b.exports:18js19) atcrypt module.注冊 (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) UnhandledPromiseRejectionWarning: 未處理的 promise 拒絕。 此錯誤源於在沒有 catch 塊的情況下拋出異步 function 內部,或拒絕未使用.catch() 處理的 promise。(拒絕 id:1)

controller:

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const db = require('../config/db.config.js')
const User = db.user
const errorHandler = require('../utils/errorHandler')

module.exports.register = async function(req, res) {
    const candidate = await User.findOne({
        where: {
            username: req.body.username
        }
    })

    if (candidate) {
        res.status(409).json({
            message: 'This login is already taken. Try another.'
        })
    } else {
        const salt = bcrypt.genSaltSync(10)
        const password = req.body.password
        const user = new User({
            name: req.body.name,
            username: req.body.username,
            roles: req.body.roles,
            photoSrc: req.file ? req.file.path: '',
            password: bcrypt.hashSync(password, salt)
        })
        try {
            await user.save()
            res.status(201).json(user)
        } catch(e) {
            errorHandler(res, e)
        }
    }
}

楷模:

module.exports = (sequelize, Sequelize) => {
    const User = sequelize.define('users', {
        name: {
            type: Sequelize.STRING(100),
            required: true
        },
        username: {
            type: Sequelize.STRING(40),
            required: true,
            unique: true
        },
        roles: {
            type: Sequelize.STRING(100),
            required: true
        },
        password: {
            type: Sequelize.STRING,
            required: true
        },
        photoSrc: {
            type: Sequelize.STRING(200),
            default: ''
        }
    });

    return User;
}

您也需要將await應用於saltpassword分配。

像這樣,

const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;

希望這可以幫助!。

對於每個異步操作,我們必須等待

const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);

像這樣為我工作,而不是 cb,你可以使用 async-await

bcrypt.genSalt(10, function(err, salt) {
  bcrypt.hash("password", salt, function(err, hash) {
    // Store hash in your password DB.
  });
});

謝謝!!

我在使用 lambda 函數時遇到了同樣的錯誤,但我的問題是解析 req 主體,所以我不得不

const body = JSON.parse(event.body) // in case of lambda function

希望這有助於某人。

當我驗證我的注冊表時發生了這種情況,我正在尋找修復程序但沒有得到正確的解決方案。

我是如何修復的后來我意識到在發送 post 請求之前我沒有通過必填字段。 在通過Postman發送Post 請求之前,確保您已經在 postman 中傳遞了key 和 value

單擊上面的鏈接查看示例。

我錯過了架構中的“OTP”字段

老的:

  Schema(
{
  email: {
    type: String,
    requird: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    index: { expires: 300 },
    // after 5 mins it get's deleted
  },
},
{ timestamps: true }

)

新的:

  Schema(
{
  email: {
    type: String,
    requird: true,
  },
  otp: {
    type: String,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    index: { expires: 300 },
    // after 5 mins it get's deleted
  },
},
{ timestamps: true }

)

暫無
暫無

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

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