Error: Illegal arguments: undefined string at bcrypt.hashSync

文章描述了在使用React进行后端开发时,遇到在生成password时的undefinedstringatbcrypt.hashSync错误。通过将bcrypt.genSaltSync和req.body.password的生成改为异步方式(await),解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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;
Output: error:exit status 1 main.java:8: error: invalid method declaration; return type required constructor() { ^ main.java:13: error: invalid method declaration; return type required connect(address, port) { ^ main.java:13: error: <identifier> expected connect(address, port) { ^ main.java:13: error: <identifier> expected connect(address, port) { ^ main.java:14: error: ')' expected return new Promise((resolve, reject) => { ^ main.java:14: error: illegal start of expression return new Promise((resolve, reject) => { ^ main.java:14: error: illegal start of expression return new Promise((resolve, reject) => { ^ main.java:16: error: ')' expected if (typeof window !== 'undefined') { ^ main.java:16: error: illegal start of expression if (typeof window !== 'undefined') { ^ main.java:16: error: unclosed character literal if (typeof window !== 'undefined') { ^ main.java:16: error: unclosed character literal if (typeof window !== 'undefined') { ^ main.java:16: error: not a statement if (typeof window !== 'undefined') { ^ main.java:17: error: illegal character: '`' this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: ';' expected this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: illegal start of expression this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: ';' expected this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:19: error: ';' expected this.connection.onopen = () => { ^ main.java:19: error: -> expected this.connection.onopen = () => { ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:24: error: illegal start of expression this.connection.onerror = (error) => { ^ main.java:24: error: illegal start of expression this.connection.onerror = (error) => { ^ main.java:30: error: class, interface, or enum expected else if (typeof require !== 'undefined') { ^ main.java:30: error: unclosed character lit... 运行结果为Output: error:exit status 1 main.java:8: error: invalid method declaration; return type required constructor() { ^ main.java:13: error: invalid method declaration; return type required connect(address, port) { ^ main.java:13: error: <identifier> expected connect(address, port) { ^ main.java:13: error: <identifier> expected connect(address, port) { ^ main.java:14: error: ')' expected return new Promise((resolve, reject) => { ^ main.java:14: error: illegal start of expression return new Promise((resolve, reject) => { ^ main.java:14: error: illegal start of expression return new Promise((resolve, reject) => { ^ main.java:16: error: ')' expected if (typeof window !== 'undefined') { ^ main.java:16: error: illegal start of expression if (typeof window !== 'undefined') { ^ main.java:16: error: unclosed character literal if (typeof window !== 'undefined') { ^ main.java:16: error: unclosed character literal if (typeof window !== 'undefined') { ^ main.java:16: error: not a statement if (typeof window !== 'undefined') { ^ main.java:17: error: illegal character: '`' this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: ';' expected this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: illegal start of expression this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:17: error: ';' expected this.connection = new WebSocket(`ws://${address}:${port}`); ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:19: error: ';' expected this.connection.onopen = () => { ^ main.java:19: error: -> expected this.connection.onopen = () => { ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:19: error: illegal start of expression this.connection.onopen = () => { ^ main.java:24: error: illegal start of expression this.connection.onerror = (error) => { ^ main.java:24: error: illegal start of expression this.connection.onerror = (error) => { ^ main.java:30: error: class, interface, or enum expected else if (typeof require !== 'undefined') { ^ main.java:30: error: unclosed character lit...
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值