4

I am trying to learn Node.js and created a simple project to query the local database. But I get failed to look up an instance error message.

I have checked that the SQL Server services running in services.msc

services.msc picture

I have verified TCP/IP is enabled

configurations manage pic

I have tried with the username and password and without it as well. I connect to localdb in SQL Server Management Studio as (localdb)\v11.0 and below is the screenshot of the properties

image of SQL Server connection properties

What am I doing incorrectly? What should be actual username and password? What should be the servername?

const sql = require('mssql');

// config for your database
const config = {
    user: 'mywindows username',
    password: 'my windows password',
    server: '(localdb)\\v11.0', 
    database: 'test',
    options: {
        encrypt: true
      } 
};
console.log('starting sql');

var connection = new sql.connect(config, function(err) {
    console.log(err);
    var request = new sql.Request(connection); 
    request.query('select * from employees', function(err, recordset) {
       if(err)      // ... error checks 
            console.log('Database connection error');

    console.dir("User Data: "+recordset);
    });
});
sql.close();
console.log('ending sql');    
});

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

Below is the error message

{ ConnectionError: Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb) at Connection.tedious.once.err (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\mssql\lib\tedious.js:244:17) at Object.onceWrapper (events.js:285:13) at Connection.emit (events.js:197:13) at InstanceLookup.instanceLookup (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\connection.js:945:16) at sender.execute (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\instance-lookup.js:66:13) at GetAddrInfoReqWrap.invokeLookupAll [as callback] (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\sender.js:43:16) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:70:17) code: 'EINSTLOOKUP', originalError: { ConnectionError: Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb) at ConnectionError (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\errors.js:13:12) at InstanceLookup.instanceLookup (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\connection.js:945:32) at sender.execute (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\instance-lookup.js:66:13) at GetAddrInfoReqWrap.invokeLookupAll [as callback] (C:\Users\vndbsubramaniam\Desktop\React projects\ReactWithSql\node_modules\tedious\lib\sender.js:43:16) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:70:17) message: 'Failed to lookup instance on (localdb) - getaddrinfo ENOTFOUND (localdb)', code: 'EINSTLOOKUP' }, name: 'ConnectionError' } Database connection error

2 Answers 2

6

After struggling for hours on this one finally found the answer here SQL to Node connection

It seems i have to add msnodesqlv8 package and use add the driver syntax to the config.

app.get('/test', (req, res) => {

const sql = require('mssql/msnodesqlv8');

// config for your database
const config = {
    database: 'test',
    server: '(localdb)\\v11.0',
    driver: 'msnodesqlv8',
    options : {
        trustedConnection : true
    }
};
console.log('starting sql');

const pool = new sql.ConnectionPool(config);
pool.connect().then(() => {
    //simple query
    pool.request().query('select * from employees', (err, result) => {
          if(err) res.send(err)
          else{
              return res.json({
                  data : result.recordset
              })
          }
      })
      sql.close();
})    
console.log('ending sql');    

});

Sign up to request clarification or add additional context in comments.

Comments

1

you will need msnodesqlv8 driver, which you have to paste it in require as

var sql = require('mssql/msnodesqlv8'),

as well as you will have to include it in driver section in config object.

var config = {
    user:"*****",
    password:"*****",
    database:"*****",
    driver: 'msnodesqlv8',
    server:"*****",
    options: {
        trustedConnection : true
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.