Skip to content

Request Error: No connection is specified for that request; while connecting to Sql server using express.js? #89

@hunaynrestarted

Description

@hunaynrestarted

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 but it throws error:

{"code":"ENOCONN","name":"RequestError"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions