You need to enable JavaScript to run this app. Node.js注册功能加密码哈希后报Cannot read property 'rows' of undefined
优惠活动
大模型
产品
解决方案
定价
更多

Node.js注册功能加密码哈希后报Cannot read property 'rows' of undefined

问题原因

你的报错TypeError: Cannot read property 'rows' of undefined 是因为await user.newUser(params) 得到的返回值为undefined,问题根源在newUser方法的异步逻辑处理错误:

  • 你混用了回调风格的bcrypt.hash和async/await语法,bcrypt.hash回调内部的return仅作用于回调函数本身,无法将查询结果返回到newUser外层,导致整个newUser方法没有返回有效值,await后拿到的就是undefined。
  • 原有newUser的catch逻辑仅打印错误,未抛出错误,外层无法捕获内部异常,会导致错误逻辑走到正常响应分支。
修复代码

替换你的newUser方法为以下实现,改用Promise版本的bcrypt调用,保证异步逻辑正确传递:

exports.newUser = async function(data){
    try {
        // 等待密码哈希生成,使用bcrypt的promise调用方式,避免回调嵌套
        const hash = await bcrypt.hash(data.password, saltRounds);
        // 注意:当前SQL拼接写法存在SQL注入风险,建议后续改为参数化查询
        const sql = `CALL "${config.DBSchemaAAM}"."sp_Users_insert"('${data.email}', '${data.lastname}', '${data.firstname}', '${data.middlename}', '${data.profilephotourl}', '${data.position}', '${data.whatyoudo}', '${data.phonenumber}', ${data.departmentid}, '${hash}')`;
        console.log(sql);
        // 等待数据库查询完成,返回查询结果
        const queryRes = await pool.query(sql);
        return queryRes;
    } catch(err) { 
        console.error('---newUser执行错误---\n', err);
        // 抛出错误,供外层调用的try/catch捕获
        throw err;
    } 
}
额外优化建议
  • SQL注入风险修复:禁止直接拼接用户输入生成SQL语句,改为数据库驱动支持的参数化查询,避免恶意输入破坏SQL结构、窃取数据。
  • 变量声明补充:你原有postUser方法里的params = req.body没有加let/const声明,会导致变量泄漏到全局作用域,建议改为const params = req.body。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 18:45:02