我正在授权数据库中的电子邮件,但是当我输入错误的电子邮件时,它抛出事务无法回滚,因为它已完成状态:提交
export const signin = async (req, res) => {
const { email, password } = req.body;
const transaction = await db.sequelize.transaction();
try {
const user = await db.User.findOne({ where: { email } }, { transaction });
await transaction.commit();
const passBool = Auth.comparePassword(password, user.password);
if (user && passBool) {
return res.json({
success: 1,
user,
message: 'signed in'
});
}
res.json({ success: 0, message: 'wrong username or password' });
} catch (ex) {
await transaction.rollback();
res.json({ success: 0, message: 'wrong username or password' });
}
};
最佳答案
我不确定为什么回滚在您的示例中不起作用,但您可以尝试:
const user = await db.User.findOne({ where: { email }, transaction });
export const signin = async (req, res) => {
const { email, password } = req.body;
db.sequelize.transaction(async transaction => {
const user = await db.User.findOne({ where: { email }, transaction });
const passBool = Auth.comparePassword(password, user.password);
if (user && passBool) {
return res.json({
success: 1,
user,
message: 'signed in'
});
}
res.json({ success: 0, message: 'wrong username or password' });
}).catch(err => {
res.json({ success: 0, message: 'wrong username or password' });
});
};
关于node.js - 当用户输入错误的电子邮件时,为什么交易失败并已回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890307/