4

I am using the latest versions of node, express and mssql module. I am trying to connect to the local instance of sql server 2014 using express.js.

I am using mssql module and callbacks mechanism from the official documentation.

I have tried:

const express= require('express');
const app= express();
const mssql= require('mssql');

app.get('/', (req, res) => 
{
  const configuration=
  {
    name: "default",
    host: 'localhost',
    database: 'HimHer',
    user: '',
    password: '',
    port: 1433
  }

        new mssql.connect(configuration, error => 
        {
            new mssql.Request().query('Select * from Users', (err, dataset) => 
            {   
                  if(err)
                  {
                      console.log(err);
                      res.send(err);  
                      return;
                  }
                  else
                  {
                      console.dir(dataset);  
                      res.send(JSON.stringify(dataset));  
                      return;           
                  }


         });
    });

    mssql.close();


    mssql.on('error', err => 
    {
        console.log(err);  
    });
});



app.listen(5000, () => 
{
    console.log('Listening to requests on port 5000');
})

I want it to be connected to the database.

CodingManiac
  • 123
  • 1
  • 1
  • 9

3 Answers3

1

I think, you close the connection before it created. Move the close connection command to next to res.send(...) lines. Then it try to connect, wait for connection, after that run select, wait for result, and after the result received send the answer to client, and close the connection.

See this example:

let x = setTimeout(function(){
    console.log('async function called');
}, 1000);
clearTimeout(x);

And see this:

let x = setTimeout(function(){
    console.log('async function called');
    clearTimeout(x);
}, 1000);

The second code will logs but the first is not.

sarkiroka
  • 1,485
  • 20
  • 28
-2

sql.Request.query() require specific the db name.

You need to specific the db in which you want to query in the Request.query() call.

add use HimHer; before the select statement.

  new mssql.connect(configuration, error => 
    {
        new mssql.Request().query('use HimHer; Select * from Users', (err, dataset) => 
        {   
              if(err)
              {
                  console.log(err);
                  res.send(err);  
                  return;
              }
              else
              {
                  console.dir(dataset);  
                  res.send(JSON.stringify(dataset));  
                  return;           
              }


     });
});
Feiga Lubow
  • 194
  • 3
  • 12
-4

As a new javascript developper i can already tell u that u should write

const app = express.Router();

instead of

const app = express(); 

when u want to use methods such as get, post, delete and so on. U have to tell ur app which way to follow depending on the situation. I've already tried without express.Router() and my applications crashed everytime. But i don't know if it will resolve ur problem and i apologize if i said something bad/Wrong i'm still learning english ^^.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Raed
  • 1
  • 2