You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

用户确认邮箱时数据库更新失败问题求助

问题修复方案

核心问题定位

报错Incorrect arguments to mysqld_stmt_execute是因为数据库更新时参数传递格式错误,同时代码存在拼写错误和逻辑冗余问题,逐一修复如下:


1. 修正数据库更新的参数传递

模型方法emailConfirmed定义接收对象参数,但调用时直接传了字符串,且SQL参数未按数组格式传递,导致执行异常:

// 模型代码修复(二选一即可)
// 方案A:修改方法参数为直接接收字符串,并确保SQL参数为数组
emailConfirmed = async(email) => {
    const sql = `UPDATE ${this.tableName} SET confirmed = true WHERE email = ?`
    const result = await query(sql, [email]) // 必须用数组包裹参数
    return result
}

// 方案B:保持方法参数为对象,调用时传递对象
// 控制器调用处改为:
const result = await userModel.emailConfirmed({ email })

2. 修正控制器的拼写与逻辑错误

confirmEmail = async (req, res, next) => {
    this.checkValidation(req)

    const { email } = req.body
    const param = req.params.token

    const user = await userModel.findOne({ email })

    if(!user) {
        throw new HttpException(401, 'User not found')
    }

    if(user.confirmed) { // 修正拼写错误:confired → confirmed
        throw new HttpException(401, 'User already confirmed')
    }

    // 移除冗余的if(!user.confirmed)判断
    const confirmJWT = jwt.verify(param, process.env.SECRET_JWT)
    if(!confirmJWT) {
        throw new HttpException(400, 'Token invalid') // 状态码改为400更符合语义
    }
    
    const result = await userModel.emailConfirmed(email) // 对应模型的参数格式
    // 验证更新是否生效
    if(result.affectedRows === 0) {
        throw new HttpException(400, 'Failed to confirm email')
    }
    // 返回成功响应,而非抛出错误
    res.status(200).json({ message: 'Email confirmed successfully' })
}

内容的提问来源于stack exchange,提问作者DMantas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 22:06:34