title: How to I return query data after committing a transaction (using knex.js)?
body: <p>I'm trying to register a user. The process is as follows - I first generate a hash for the user, then generate a token. Assuming these 2 steps are successful, I then add the user to the database. </p>
  
  <p>This step involves adding the user to 2 tables - a user_credentials table containing just the hash, and using the id returned after this operation to add the other user details and token to a users table.</p>
  
  <p>I am able to successfully add the user - however, when I try to access the user data returned by knex after adding the user to both tables, I always get <code>Data: undefined</code>.</p>
  
  <p>Not sure what's the correct way to send the row data back from createUser(), and have it available in the next then block (where it's console logged).</p>
  
  <pre><code>const handleRegister = (req, res, db, bcrypt, saltRounds) =&gt; {
      // Get the details from the body
      let user = req.body;
      // Hash the plain text password using bcrypt
      bcrypt.hash(user.password, saltRounds)
          .then((hash) =&gt; {
              delete user.password;
              // set user's hash to encrypted pw
              user.hash = hash; 
          })
          // Create token to be sent back to the client to create a session
          .then(() =&gt; createToken())
          // Set user's token to created token
          .then((token)=&gt; {
              user.token = token;
          })
          // Save hashed password to db for the user and save user data to db with created token
          .then(() =&gt; createUser(user, db))
          // Return info of created user
          .then((data) =&gt; {
              delete user.hash;
              console.log("User:", user);
              console.log("Data:", data);
              res.status(201).json(user);
          })
          .catch((err) =&gt; {
              console.log("This was the error:", err);
              res.status(400).json("Something went wrong");
          });
  }
  
  
  // creating a user in the database
  const createUser = (user, db) =&gt; {
      return db.transaction((trx) =&gt; {
          trx.insert({
              hash: user.hash
          })
          .into('user_credentials')
          .returning('id')
          .then((userId) =&gt; {
              return trx('users')
                  .returning('*')
                  .insert({
                      user_id: userId[0],
                      email: user.email,
                      name: user.name,
                      token: user.token,
                      created_at: new Date()
                  });
          })
          .then((data)=&gt; {
              return trx.commit()
              .then(() =&gt; data);
          })
          .catch((err) =&gt; {
              trx.rollback();
              throw err;
          })
      })
  }
  </code></pre>