gpt4 book ai didi

node.js - 当用户输入错误的电子邮件时,为什么交易失败并已回滚

转载 作者:行者123 更新时间:2023-12-03 22:37:38 25 4
gpt4 key购买 nike

我正在授权数据库中的电子邮件,但是当我输入错误的电子邮件时,它抛出事务无法回滚,因为它已完成状态:提交

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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com