簡體   English   中英

無法使用“bcryptjs”-> 控制台“錯誤:非法 arguments:數字,字符串”來 hash 密碼

[英]Unable to hash passwords with “bcryptjs” -> Console “Error: Illegal arguments: number, string”

我在 Node/Express 服務器中使用 bcryptjs 在后端散列用戶密碼時遇到問題。 當我嘗試按照官方文檔使用它時,我收到上述錯誤。 我嘗試了不同的方法,但沒有任何解決方案。 請你能給我一些想法嗎?

這是哈希碼

        const Usuario = require('../models/Usuario');
        const bcryptjs = require('bcryptjs');

        usuario = new Usuario(req.body);

        //Hashear Password
        const salt = await bcryptjs.genSalt(10);
        usuario.password = await bcryptjs.hash(password, salt);

        //Guardar Usuario
        await usuario.save();
        //Mensaje Confirmacion
        res.json({ msg: 'El usuario fue creado correctamente' });

這是用戶 model

const mongoose = require('mongoose');

const UsuariosSchema = mongoose.Schema({
    nombre: {
        type: String,
        required: true,
        trim: true
    },
    email: {
        type: String,
        required: true,
        trim: true,
        unique: true
    },
    password: {
        type: String,
        required: true,
        trim: true
    },
    registro: {
        type: Date,
        default: Date.now()
    }
});

module.exports = mongoose.model('Usuario', UsuariosSchema);

提前致謝!!

根據他們的文檔,傳遞給 bcryptjs.hash 的變量password必須是字符串類型。

正如你上面給出的剪斷,無法識別密碼的聲明地點和時間及其類型。

我嘗試了以下代碼並收到錯誤“非法參數:數字,字符串”

var password = 1234;
const salt = await bcryptjs.genSalt(10);
var pass= await bcryptjs.hash(password, salt);

其中下面的代碼有效

var password = "1234";
const salt = await bcryptjs.genSalt(10);
var pass= await bcryptjs.hash(password, salt);
console.log(pass);

Output: 在此處輸入圖像描述

您能否檢查傳遞給 hash function 的類型值。

暫無
暫無

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

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