Error: Illegal arguments: undefined string at bcrypt.hashSync

用react写后端的signUp时出现报错undefined string at bcrypt.hashSync,代码如下,报错在生成password时!

复制代码
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)
        }
    }
}

修改方式,在生成salt和password时,使用await,因为本身就是异步运行:

复制代码
const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;
相关推荐
摇滚侠1 分钟前
JavaWeb 全套教程 Filter 107-111
java·开发语言·servlet
Dontla1 分钟前
HTML实体转义(HTML Entity Escaping)介绍
前端·html
咸鱼翻身小阿橙2 分钟前
高斯模糊降噪/磨皮算法降噪图像
前端·opencv·算法·webpack·c#
聆风吟º4 分钟前
【C标准库】深入理解C语言 atoi 函数:字符串转换为整数
c语言·开发语言·库函数·atoi
ct9784 分钟前
ES6 新特性
前端·vue.js·性能优化
凤山老林4 分钟前
81-Java Scanner 类
java·开发语言
j_xxx404_4 分钟前
MySQL数据库基础硬核解析:从 C/S 网络服务到磁盘文件与存储引擎
linux·运维·服务器·开发语言·数据库·mysql·ai
艾莉丝努力练剑5 分钟前
【QT】系统相关:QT文件
linux·服务器·开发语言·网络·qt·tcp/ip·计算机网络
沐苏瑶7 分钟前
深入浅出 Java 文件操作与 IO:从文件系统到数据流实战
java·开发语言
海鸥-w8 分钟前
用python (fastapi)做项目第二天实现新闻列表和新闻详情接口
开发语言·python·fastapi