title: Function not returning a recordset
body: <p><strong>EDIT: 
  Answer is at the end of this question, because I couldn't answer the question due to the fact that it was marked as a duplicate.</strong></p>
  
  <p>I'm new to node and express and am facing a problem while trying to retrieve a recordset from a database.</p>
  
  <p>I have a function in my code that is supposed to get a recordset from a db: </p>
  
  <pre><code>// Connection
  const db_connection = nconf.get("db");
  const connectionPool = new mssql.ConnectionPool(db_connection).connect(err =&gt; {
    if (err)
      failureCallback(err);
  });
  
  // The function I'm talking about
  function getAllUser() {
    connectionPool.query("SELECT * FROM User", (err, result) =&gt; {
      if (err)
        failureCallback(err);
      else
        return result.recordset;
    });
  }
  </code></pre>
  
  <p>I call this function in my routing at: </p>
  
  <pre><code>app.get("/users", (req, res) =&gt; {
    res.send(getAllUser());
  });
  </code></pre>
  
  <p>The result is... nothing. A blank page - no error.</p>
  
  <p>If I use a function like this: </p>
  
  <pre><code>function getAllUser2(callback) {
    connectionPool.connect().then(function () {
      const request = new mssql.Request(connectionPool);
      request.query("SELECT * FROM User").then(function (result) {
        connectionPool.close();
        callback(result.recordset);
      }, failureCallback);
    }, failureCallback);
  }
  </code></pre>
  
  <p>And in routing:</p>
  
  <pre><code>app.get("/users2", (req, res) =&gt; {
    conn.getAllUser(function (err, result) {
      if (err) 
        res.send(err);
      else 
        res.send(result);
    });
  });
  </code></pre>
  
  <p>It works. To my understanding, I didn't change much, I just tried to make it more readable/  understandable. </p>
  
  <p>I am grateful for any help and/ or guidance!</p>
  
  <p><strong><em>EDIT</em></strong>:
  Although the question got marked as a duplicate and after reading the question/ answer linked to it, I still got the problem and no idea why it's not working. So if anyone can help me I would be really grateful...</p>
  
  <p>With a function like this: </p>
  
  <pre><code>test5 = function () {
    return connectionPool.query("SELECT * FROM Objekt", (err, result) =&gt; {
      if (err)
        failureCallback(err);
      else {
        console.log("Test 5 success!");
        console.log(result);
        //console.log(recordset);
        console.log(result.recordset);
        //result.recordset;
        result;
      }
    })
  };
  </code></pre>
  
  <p>I get this on the page: </p>
  
  <pre><code>TypeError: Converting circular structure to JSON
      at JSON.stringify (&lt;anonymous&gt;)
      at stringify (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\response.js:1123:12)
      at ServerResponse.json (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\response.js:260:14)
      at ServerResponse.send (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\response.js:158:21)
      at app.get (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\app.js:110:7)
      at Layer.handle [as handle_request] (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\router\layer.js:95:5)
      at next (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\router\route.js:137:13)
      at Route.dispatch (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\router\route.js:112:3)
      at Layer.handle [as handle_request] (M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\router\layer.js:95:5)
      at M:\MyFiles\Workspaces\Node_workspace\SPPA\API\node_modules\express\lib\router\index.js:281:22
  </code></pre>
  
  <p>But the correct result in console (console.log(result)):</p>
  
  <pre><code>{ recordsets: [ [ [Object], [Object], [Object], [Object] ] ],
    recordset:
    [ { id: '3FCB9E85-CEB1-41F1-84B4-4A9912345678',
    anmeldename: 'lustig_pe',
    email: 'peter.lustig@company.com',
    istAdmin: false },
  { id: '04B142F1-1A5C-4B97-A173-650C12345678',
    anmeldename: 'durden_ty',
    email: 'tylor.durden@company.com',
    istAdmin: true },
  { id: 'A5019258-2CC7-4794-A924-65F812345678',
    anmeldename: 'sarpei_ha',
    email: 'hans.sarpei@company.com',
    istAdmin: true },
  { id: 'BAFF838E-B606-4DC8-AEF6-DED612345678',
    anmeldename: 'mustermann_ma',
    email: 'max.mustermann@company.com',
    istAdmin: false } ],
    output: {},
    rowsAffected: [ 4 ] }
  [ { id: '3FCB9E85-CEB1-41F1-84B4-4A9912345678',
      anmeldename: 'lustig_pe',
      email: 'peter.lustig@company.com',
      istAdmin: false },
    { id: '04B142F1-1A5C-4B97-A173-650C12345678',
      anmeldename: 'durden_ty',
      email: 'tylor.durden@company.com',
      istAdmin: true },
    { id: 'A5019258-2CC7-4794-A924-65F812345678',
      anmeldename: 'sarpei_ha',
      email: 'hans.sarpei@company.com',
      istAdmin: true },
    { id: 'BAFF838E-B606-4DC8-AEF6-DED612345678',
      anmeldename: 'mustermann_ma',
      email: 'max.mustermann@company.com',
      istAdmin: false } ]
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT: 
  I found a solution but I can't answer my question (I guess because it is marked as a duplicate), so I will show it here in the question:</strong> </p>
  
  <pre><code>// This is the function you want to call in the routing:
  function getFoo() {
    try {
      return connectionPool.query("SELECT * FROM Foo");
    }
    catch (err) {
      return err.message;
    }
  }
  
  // In routing you have to add **async** to the callback function 
  // and add **await** next to the function you are calling. 
  // This works now!
  app.get('/v2/anhang', async (req, res) =&gt; {
    res.send(await getFoo());
  });
  </code></pre>
  
  <p>I also want to point out that the linked question (with its answers) to this seemingly "duplicate" question did not help me at all. I tried so many things in the function that I was trying to call in the routing, that I forgot about the callback function <strong>in</strong> the routing... So I hope this will help beginners with a similar problem, so that they dont have to read mostly unrelated stuff and waste their time.</p>