Nodejs: TypeError: Cannot read property 'rows' of undefined hashing password
Nodejs: TypeError: Cannot read property 'rows' of undefined hashing password
我正在尝试在我的注册中散列密码,起初当我没有散列码时,代码可以工作但是当我添加散列码功能时,我收到这个错误,请检查下图,
async function postUser(req, res, next){
params = req.body;//post
let validation_rules = {
email: 'email|required',
firstname: 'required',
lastname: 'required',
middlename: 'required',
position: 'required',
departmentid: 'required|integer',
phonenumber: 'required',
whatyoudo: 'required',
password: 'required|confirmed|min:8',
};
let validation = new Validator(params, validation_rules);
console.log("params", params);
if(validation.passes())
{
try
{
params.profilephotourl = "photourlsoon";
let results = await user.newUser(params);
res.status(200).json(results.rows);
}catch(err){
console.log("-----result data error-----\n", err);
res.status(500).json(err);
}
}else{
res.status(400).json(validation.errors);
}
}
//
exports.newUser = async function(data){
try {
bcrypt.hash(data.password, saltRounds, function(err, hash) {
let 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);
return pool.query(sql,)
.then((res) => { return res;})
.catch((err) => { return console.error('---Error executing query---\n', err.stack) });
});
} catch(err) { return console.error(err); }
}
错误
TypeError: Cannot read property 'rows' of undefined
错误发生是因为您在调用 bcrypt.hash
函数时使用了回调函数,因此您的函数 newUser
returns undefined
是预期的。
这里是重写函数的方法。
exports.newUser = async function(data) {
try {
return bcrypt.hash(data.password, saltRounds).then((hash) => {
let 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);
return pool.query(sql,)
.catch((err) => { return console.error('---Error executing query---\n', err.stack) });
});
} catch(err) { return console.error(err); }
}
也与问题无关,但值得一提的是,你不应该只是将从用户那里收到的数据放入 SQL 查询中,你应该使用准备好的语句,否则你可能会受到 SQL 注射.
我正在尝试在我的注册中散列密码,起初当我没有散列码时,代码可以工作但是当我添加散列码功能时,我收到这个错误,请检查下图,
async function postUser(req, res, next){
params = req.body;//post
let validation_rules = {
email: 'email|required',
firstname: 'required',
lastname: 'required',
middlename: 'required',
position: 'required',
departmentid: 'required|integer',
phonenumber: 'required',
whatyoudo: 'required',
password: 'required|confirmed|min:8',
};
let validation = new Validator(params, validation_rules);
console.log("params", params);
if(validation.passes())
{
try
{
params.profilephotourl = "photourlsoon";
let results = await user.newUser(params);
res.status(200).json(results.rows);
}catch(err){
console.log("-----result data error-----\n", err);
res.status(500).json(err);
}
}else{
res.status(400).json(validation.errors);
}
}
//
exports.newUser = async function(data){
try {
bcrypt.hash(data.password, saltRounds, function(err, hash) {
let 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);
return pool.query(sql,)
.then((res) => { return res;})
.catch((err) => { return console.error('---Error executing query---\n', err.stack) });
});
} catch(err) { return console.error(err); }
}
错误
TypeError: Cannot read property 'rows' of undefined
错误发生是因为您在调用 bcrypt.hash
函数时使用了回调函数,因此您的函数 newUser
returns undefined
是预期的。
这里是重写函数的方法。
exports.newUser = async function(data) {
try {
return bcrypt.hash(data.password, saltRounds).then((hash) => {
let 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);
return pool.query(sql,)
.catch((err) => { return console.error('---Error executing query---\n', err.stack) });
});
} catch(err) { return console.error(err); }
}
也与问题无关,但值得一提的是,你不应该只是将从用户那里收到的数据放入 SQL 查询中,你应该使用准备好的语句,否则你可能会受到 SQL 注射.