title: Nodejs async/await mysql queries
body: <p>I have a nodejs project with the current structure below, I need to insert a registry on clients table and return the last inserted ID from this table so I can use it in a second table, but I need to wait until the insert is completed in clients table, before insert the client ID on my second table. I'm trying to use async/await, but I'm always getting a null value.</p>
  
  <p>My MYSQL connection: db.model.js</p>
  
  <pre><code>const config = require('config');
  const mysql = require("mysql");
  
  const connection = mysql.createConnection({
      host: config.get('mysql.host'),
      user: config.get('mysql.user'),
      password: config.get('mysql.password'),
      database: config.get('mysql.database')
  });
  
  connection.connect(function (err) {
      if (err) {
          console.error(`MySQL Connection Error: ${err.stack}`);
          return;
      }
  
      console.log(`MySQL connected successfully!`);
  });
  
  module.exports = connection;
  </code></pre>
  
  <p>My CLIENT model</p>
  
  <pre><code>const mysql = require("./db.model");
  
  const Client = function(client) {
      this.login = client.login;
  };
  
  Client.create = (newClient, result) =&gt; {
      mysql.query("INSERT INTO clients SET ?", newClient, 
          (err, res) =&gt; {
              if (err) {
                  console.log("error: ", err);
                  result(err, null);
                  return;
              }
  
              result(null, {
                  id: res.insertId,
                  ...newClient
              });
          }
      );
  };
  
  module.exports = Client;
  </code></pre>
  
  <p>this is the client controller (i'm trying to use async/await here)</p>
  
  <pre><code>const Client = require('../models/client.model');
  
  exports.create = (login) =&gt; {
      const client = new Client({
          login: login
      });
  
      Client.create(client, async (err, data) =&gt; {
          if(!err) {
              return await data.id;
          } else {
              return false;
          }
      });
  }
  </code></pre>
  
  <p>And this is another controller, where I want to use methods from my client controller:</p>
  
  <pre><code>
  const ClientController = require('../controllers/client.controller');
  
  ...
  
      utils.connect()
          .then(clt =&gt; clt.sub.create(data))
          .then((sub) =&gt; {
  
              let lastInsertedId = ClientController.create(sub.login);
  
              // lastInsertedId always return null here, 
              // but I know ClientController return a value after some time.
              // method below will fail because lastInsertedId cannot be null
              TransactionController.transactionCreate(lastInsertedId,
                                                      sub.id, 
                                                      sub.param);
          })
          .catch(error =&gt; res.send(error.response.errors))
  
  </code></pre>
  
  <p>any help appreciated.</p>