Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
Express route works fine, until it calls separate function, crashes server

The first GET request, (/not-work), calls a separate function, database(), with some code to connect to a database and return data. This successfully runs the database() function and that function returns data, but const result, does not get that data properly returned. Instead an unfufilled database connection object is returned. The second request however, (/works), runs nearly the exact same code as, just within the GET route itself. This works perfectly. I cannot for the life of me understand why.

const http = require('http');
const express = require('express');
const app = express();
const sql = require('mssql')
const sqlConfig = {
    user: 'login',
    password: 'pass',
    database: 'database',
    server: '123.456.789.012'
}

const database = () => {
    return sql.connect(sqlConfig,
        (err) => {
            if (err) console.log(err);
            try {
                return new sql.Request()
                    .execute('dbo.SQLProcedure', (err, recordset) => {
                        if (err) console.log(err)
                        return recordset;
                    });
            }
            catch (err) {
                console.log(err)
            }
        });
}

app.get('/not-work', (req, res) => {
    const result = database();
    console.log(result);
    res.send(result);
});

app.get('/works', (req, res) => {
    sql.connect(sqlConfig,
        (err) => {
            if (err) console.log(err);
            try {
                new sql.Request()
                    .execute('dbo.SQLProcedure', (err, recordset) => {
                        if (err) console.log(err)
                        res.send(recordset);
                    });
            }
            catch (err) {
                console.log(err)
                res.send(err);
            }
        });
});

http.createServer(app).listen(888, () => {
    console.log('Server listening 888');
});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you must use async function and Promise

const database = async () => {
  return new Promise( (resolve, reject) => {
    sql.connect(sqlConfig,
      (err) => {
        if (err) console.log(err); // or reject(err)
        try {
          new sql.Request()
            .execute('dbo.SQLProcedure', (err, recordset) => {
              if (err) console.log(err) // or reject(err)
              return resolve(recordset);
            });
        } catch (err) {
          console.log(err) // or reject(err)
        }
      });
  })
}

app.get('/not-work', async (req, res) => {
    const result = await database();
    console.log(result);
    res.send(result);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!