我无法将此代码回调函数转换为承诺。当我将原型转换为正常功能时,它给了我一个错误,请帮我修复这个错误。我正在尝试使用 ES7 异步等待功能来避免回调。
function User() {};
User.prototype = {
find : function(user = null, callback)
{
if(user) {
let field = Number.isInteger(user) ? 'id' : 'username';
}
let sql = `SELECT * FROM users WHERE ${field} = ?`;
pool.query(sql, user, function(err, result) {
if(err) throw err
if(result.length) {
callback(result[0]);
}else {
callback(null);
}
});
},
create : function(body, callback)
{
let pwd = body.password;
body.password = bcrypt.hashSync(pwd,10);
let bind = [];
for(prop in body){
bind.push(body[prop]);
}
// prepare the sql query
let sql = `INSERT INTO users( fullname,email,birthday,gender,country,city,state,mobile,password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
pool.query(sql, bind, function(err, result) {
if(err) throw err;
callback(result.insertId);
});
},
login : function(username, password, callback)
{
this.find(username, function(user) {
if(user) {
if(bcrypt.compareSync(password, user.password)) {
callback(user);
return;
}
}
callback(null);
});
}
}
module.exports = User;```
胡子哥哥
相关分类