title: Getting Data from Azure SQL database in Azure Function
body: <p>I'm having problems getting data from my AZURE SQL database. My code does get data, but not all of it. The intention is that the function needs to take all users in wicht the age is X (f.ex.:20)and return an array with those users. Right now the code just return the first user it finds on the database. I am using Azure-functions in which I use Insomnia to test the result.
  Here is the function that gets the data from the DB:</p>
  <pre><code>function testfunc(age){
      return new Promise ((resolve, reject) =&gt;{
          let result = [];
          const sql = 'SELECT * FROM [datingschema].[user] where age = @age'
          const request = new Request(sql, function(err){
              if (err){
                  console.log(&quot;beforeerr&quot;);
                  console.log(err) //ingen err - så det godt nok!
                  console.log(&quot;aftererr&quot;);
                  reject(err);
              }
          })
          request.addParameter('age', TYPES.Int, age)
          request.on('row', (columns) =&gt; {
              columns.forEach(column =&gt;{
  
                  result.push(column.value)
              })
              resolve(result)
          });
          
      
          connection.execSql(request)
      })
  }
  </code></pre>
  <p>Here is a part of my code in Azure-function where I call for the function. There should be no errors in there as it works fine when I need to get only one user:</p>
  <pre><code>const db = require('../database/db');
  
  
  module.exports = async function (context, req) {
      context.log('JavaScript HTTP trigger function processed a request.')
  
      try {
          await db.startDb(); //start db connection
      } catch (error) {
          console.log(&quot;Error connecting to the database&quot;, error.message)
      }
      switch (req.method) {
          case 'GET':
              await get(context, req);
              break;
          case 'POST':
              await post(context, req);
              break
          default:
              context.res = {
                  body: &quot;Please get or post&quot;
          };
              break
      }
  }
  
  async function get(context, req){
      try{
          let id = req.query.age
          let user = await db.testfunc(id)
          context.res = {
              body: user
          };
      } catch(error){
          context.res = {
              status: 400,
              body: `No User - ${error.message}`
          }
      }
  }
  </code></pre>