我有这个功能可以将用户保存在数据库中:
exports.saveUser = ({ first_name, last_name, email, password }) => {
const query = "insert into users (first_name, last_name, email, password_hash) values ($1, $2, $3, $4) RETURNING *";
bcrypt.hash(password, 10, async function (err, hash) {
const res = await db.query(query, [first_name, last_name, email, hash]);
return res.rows[0];
});
return res;
};
我在 bcrypt 回调中有异步函数,其中发生插入以及我返回插入的行。我如何让它返回 saveUser 函数?
所以最后
saveUser()
会是那个res.rows[0]
记录吗?
exports.saveUser = async ({ first_name, last_name, email, password }) => {
const query = "insert into users (first_name, last_name, email, password_hash) values ($1, $2, $3, $4) RETURNING *";
let hash = await bcrypt.hash(password, 10);
const res = await db.query(query, [first_name, last_name, email, hash]);
return res.rows[0];
};