Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何验证Sequelize事务并使其看起来美观_Javascript_Node.js_Validation_Transactions_Sequelize.js - Fatal编程技术网

Javascript 如何验证Sequelize事务并使其看起来美观

Javascript 如何验证Sequelize事务并使其看起来美观,javascript,node.js,validation,transactions,sequelize.js,Javascript,Node.js,Validation,Transactions,Sequelize.js,在放下NodeJS几年后,我试图重新学习它,所以我正在建立一个小型银行网站作为测试。我决定在我的ORM中使用Sequelize,但我在以我喜欢的方式在人与人之间汇款时遇到了一些麻烦。 这是我的第一次尝试: // myUsername - who to take the money from // sendUsername - who to send the money to // money - amount of money to be sent from `myUsername`->`

在放下NodeJS几年后,我试图重新学习它,所以我正在建立一个小型银行网站作为测试。我决定在我的ORM中使用Sequelize,但我在以我喜欢的方式在人与人之间汇款时遇到了一些麻烦。 这是我的第一次尝试:

// myUsername - who to take the money from
// sendUsername - who to send the money to
// money - amount of money to be sent from `myUsername`->`sendUsername`
// Transaction is created to keep a log of banking transactions for record-keeping.
module.exports = (myUsername, sendUsername, money, done) => {
    // Create transaction so that errors will roll back
    connection.transaction(t => {
        return Promise.all([
            User.increment('balance', {
                by: money,
                where: { username: myUsername },
                transaction: t
            }),
            User.increment('balance', {
                by: -money,
                where: { username: sendUsername },
                transaction: t
            }),
            Transaction.create({
                fromUser: myUsername,
                toUser: sendUsername,
                value: money
            }, { transaction: t })
        ]);
    }).then(result => {
        return done(null);
    }).catch(err => {
        return done(err);
    });
};
这是可行的,但当模型增加时,它没有验证模型。我希望在模型未验证时事务失败。我的下一次尝试是转到回调,如下所示(相同的函数头):

这是可行的,因为钱仍然在人与人之间转移,但它仍然不能验证模型。我认为原因是
.validate()
.reload()
方法无法在其上添加
事务:t
参数。 我的问题是,是否有一种方法可以在事务中进行验证,但我也想得到一些帮助来修复这个“回调地狱”。再说一次,我已经有一段时间没有做JS了,所以我现在知道可能有更好的方法来做这件事


谢谢

我相信您无法对
模型
增量
减量
进行验证,并且需要有实例。在某些sequelize模型方法中,您可以配置要运行的验证,但它不能

我会这样做

module.exports = async function(myUserId, sendUserId, money) {
  const transaction = await connection.transaction();
  try {
    const [myUser, sendUser] = await Promise.all([
      User.findById(myUserId, { transaction }),
      User.findById(sendUserId, { transaction })
    ]);
    await Promise.all([
      myUser.increment('balance', {
        by: money,
        transaction
      }),
      myUser.increment('balance', {
        by: -money,
        transaction
      })
    ]);
    await Transaction.create({...}, { transaction })
    await transaction.commit();
  } catch(e) {
    await transaction.rollback();
    throw e;
  }
}
module.exports = async function(myUserId, sendUserId, money) {
  const transaction = await connection.transaction();
  try {
    const [myUser, sendUser] = await Promise.all([
      User.findById(myUserId, { transaction }),
      User.findById(sendUserId, { transaction })
    ]);
    await Promise.all([
      myUser.increment('balance', {
        by: money,
        transaction
      }),
      myUser.increment('balance', {
        by: -money,
        transaction
      })
    ]);
    await Transaction.create({...}, { transaction })
    await transaction.commit();
  } catch(e) {
    await transaction.rollback();
    throw e;
  }
}