title: Tedious Requests can only be made in the LoggedIn state
body: <p>i'm trying to get some SQL connections to go on, but everytime I'm making a new request it keeps throwing an error:</p>
  <p>RequestError: Requests can only be made in the LoggedIn state, not the Final state</p>
  <p>I'm basically using these 3 functions for requests:</p>
  <pre><code>function executeStatementMultipleInsert(array) {
    request = new Request(
      `INSERT INTO tbs_nome (nome, cod)
          VALUES (@nome, @nome_cod)
          INSERT INTO tbs_sobrenome (sobrenome, cod) 
          VALUES (@sobrenome, @sobrenome_cod)
          INSERT INTO tbs_email (email, cod) 
          VALUES (@email, @email_cod)
        `,
      function (err) {
        if (err) {
          console.log(err);
        }
      }
    );
    request.addParameter(&quot;nome&quot;, TYPES.NVarChar, array[0].nome);
    request.addParameter(&quot;nome_cod&quot;, TYPES.BigInt, array[0].cod);
    request.addParameter(&quot;sobrenome&quot;, TYPES.NVarChar, array[1].sobrenome);
    request.addParameter(&quot;sobrenome_cod&quot;, TYPES.BigInt, array[1].cod);
    request.addParameter(&quot;email&quot;, TYPES.NVarChar, array[2].email);
    request.addParameter(&quot;email_cod&quot;, TYPES.BigInt, array[2].cod);
    request.on(&quot;requestCompleted&quot;, () =&gt; {
      databaseConnection.close();
    });
    databaseConnection.execSql(request);
  }
  
  // Function to get the soma from queries
  
  async function getSoma(tableName, cod) {
    return tp
      .sql(
        `DECLARE @tableName varchar(1000)
    SET @tableName = 'SELECT soma from tbs_cod_${tableName} WHERE cod=${cod}'
    EXEC (@tableName)`
      )
      .execute()
      .then((results) =&gt; {
        // do something with the results
        // console.log(results);
  
        databaseConnection.close();
        // console.log(&quot;database was closed&quot;);
  
        return results[0].soma;
      })
      .fail(function (err) {
        // do something with the failure
        console.log(&quot;nay&quot; + err);
      });
  }
  
  async function getFinalResults(total) {
    console.log(total);
    let result = &quot;&quot;;
    request = new Request(
      `SELECT a.animal, c.cor, p.pais 
      FROM tbs_animais AS a
      JOIN tbs_cores AS c ON a.total=c.total
      JOIN tbs_paises AS p ON a.total=p.total
      LEFT JOIN tbs_cores_excluidas AS ce ON a.total=ce.total
      WHERE a.total=@total`,
      function (err) {
        console.log(&quot;hey db&quot;);
        databaseConnection.close();
        if (err) {
          throw err;
        }
      }
    );
    request.addParameter(&quot;total&quot;, TYPES.BigInt, total);
    request.on('row', function(columns) {  
      columns.forEach(function(column) {  
        if (column.value === null) {  
          console.log('NULL');  
        } else {  
          console.log('not null');
          result+= column.value + &quot; &quot;;  
        }  
      });  
      console.log('hey');
      console.log('the result is ' + result);  
      result =&quot;&quot;;  
  });  
  request.on('done', function(rowCount, more) {  
    console.log(rowCount + ' rows returned');  
    });
    databaseConnection.execSql(request);
  }
  
  const databaseConnection = new Connection(config);
  
  </code></pre>
  <p>while in my app.js I call them in some chain then statements. only the first post of the server gets inserted correctly, and when I get to my final request, a SELECT in the &quot;getFinalResults&quot; function, it throws me that error. This is some of my app.js file</p>
  <pre><code>app.get(&quot;/&quot;, (req, res) =&gt; {
    res.render(&quot;index&quot;);
  });
  
  // Post submit route
  app.post(&quot;/submit&quot;, (req, res) =&gt; {
    let codesArray = [
      {
        nome: req.body.nome,
      },
      {
        sobrenome: req.body.sobrenome,
      },
      {
        email: req.body.email,
      },
    ];
    let codesSum;
    // post para a API para obter o array de código
    axios
      .post(&quot;http://138.68.29.250:8082&quot;, qs.stringify(req.body), {
        headers: { &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot; },
      })
      .then(async (response) =&gt; {
        codesSum = response.data.split(/[A-Za-z]*#/gi).filter(Boolean);
        codesArray.map((element, index) =&gt; {
          return (element.cod = Number(codesSum[index]));
        });
        // executeStatementMultipleInsert(codesArray);
        console.log(codesArray);
      })
      .then(async () =&gt; {
        for (let object of codesArray) {
          let soma = await getSoma(Object.keys(object)[0], object.cod);
          // console.log(soma);
          codesSum.push(soma);
        }
        // console.log(codesSum);
        let total = await codesSum
          .map((num) =&gt; Number(num))
          .reduce((acc, cv) =&gt; acc + cv);
          console.log(typeof total);
        let objectFinal = await getFinalResults(total);
        
        // console.log(objectFinal);
      })
      .catch((err) =&gt; {throw err});
    // connection to database
    databaseConnection.connect((err) =&gt; {
      if (err) {
        console.log(&quot;Connection failed&quot;);
        throw err;
      }
    });
  
    res.redirect(&quot;/&quot;);
  });
  </code></pre>
  <p>i'm still having lots of trouble with asynchronicity and I suspect some of the issure might come from there? Or when I'm connecting and closing the connection to the database, which I'm also not sure is entirely correct</p>