最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to return a newly created object when using sequelize transcation? - Stack Overflow

programmeradmin1浏览0评论

I have a following function, which works, when passed a new user data . it saves the object and the child object into the mysql table successfully. but how do i return the object back , once saved to the database, given i'm using sequelize transaction.

static async add(user) {
    let transaction; 
      try {
          // get transaction
        transaction = await models.sequelize.transaction();

        //  *****how to return the newly created user *****************************************

        models.User.create(user).then(newUser => {
          const id = newUser.id;

           //save address 
          if(user.address){

              address.userId = id;
              models.Address.create(address); 

          }
        }).catch(error => {
          throw error;
        }); 

        await transactionmit();

      } catch (error) {
          console.log(error); 
            // Rollback transaction 
          if (transaction) await transaction.rollback();
          throw error;
      }
    }

I have a following function, which works, when passed a new user data . it saves the object and the child object into the mysql table successfully. but how do i return the object back , once saved to the database, given i'm using sequelize transaction.

static async add(user) {
    let transaction; 
      try {
          // get transaction
        transaction = await models.sequelize.transaction();

        //  *****how to return the newly created user *****************************************

        models.User.create(user).then(newUser => {
          const id = newUser.id;

           //save address 
          if(user.address){

              address.userId = id;
              models.Address.create(address); 

          }
        }).catch(error => {
          throw error;
        }); 

        await transaction.mit();

      } catch (error) {
          console.log(error); 
            // Rollback transaction 
          if (transaction) await transaction.rollback();
          throw error;
      }
    }
Share Improve this question asked May 27, 2020 at 21:24 arvearve 8114 gold badges19 silver badges38 bronze badges 5
  • Does this answer your question? How do I return the response from an asynchronous call? – ggorlen Commented May 27, 2020 at 21:27
  • 1 You just mixed up two approaches: async/await and then chain. I remend to use async/await and call models.User.create(user) as const newUser = await models.User.create(user). Also don't forget to pass a transaction object to all sequelize queries inside it explicitly like this: models.User.create(user, { transaction }) – Anatoly Commented May 27, 2020 at 21:40
  • 1 @Anatoly - thanks. with this const newUser = await models.User.create(user, {transaction}); I don't have to await transaction. I need to create/save address object as well, once user is created. How do I do that with aysnc/await. would you mind give me sample code snippet? – arve Commented May 31, 2020 at 20:28
  • 2 if(user.address){ address.userId = id; await models.Address.create(address, { transaction }); } – Anatoly Commented May 31, 2020 at 21:58
  • @Anatoly - I know this is an old question but, I am still not sure if I'm doing this correctly. can you post plete code sample , on creating user , then address and returning the user object at the end. – ozil Commented Aug 12, 2020 at 22:57
Add a ment  | 

1 Answer 1

Reset to default 3

Try to create an auto-transaction, use await and indicate transaction in models's create functions:

static async add(user) {
      try {
        const createdUser = await models.sequelize.transaction(transaction => {
          const newUser = await models.User.create(user, { transaction })
           //save address 
          if(user.address){
            address.userId = newUser.id;
            await models.Address.create(address, { transaction }); 
          }
          return newUser;
        });
        // if you are here it means transaction already mited
        return createdUser;
      } catch (error) {
          // if you are here it means transaction already rolled back
          console.log(error); 
          throw error;
      }
    }
发布评论

评论列表(0)

  1. 暂无评论