2

I am trying to setup connection with MS SQL but for some reason, sequelize is not able to pass the hostname correctly, Instead of pass 'localhost\SQLEXPRESS' it pass 'localhostSQLEXPRESS'. Any idea where to fix it ?

'use strict';
const _ = require('lodash');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('mydb', 'db_user', 'db_user', {
    host: 'localhost\SQLEXPRESS',
    dialect: 'mssql',
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
});
// define model 
var batchDetails = sequelize.define('batchDetails', {
    id: {
        type: Sequelize.STRING,
        autoIncrement: true,
        field:'id',
        primaryKey: true
    },
    batch_no: {
        type: Sequelize.STRING,
        field:'batch_no',
    },
    date: {
        type: Sequelize.DATE,
        field:'date',
    }
})

batchDetails.sync({force: true}).then(function () {
    // Table created
    return User.batchDetails({
      id: 1,
      batch_no: 'CASFR342'
    });
  });

error log:-

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules\sequelize\lib\sequelize.js:242:13 tedious deprecated The default value for options.encrypt will change from false to true. Please pass false explicitly if you want to retain current behaviour. node_modules\sequelize\lib\dialects\mssql\connection-manager.js:69:26 Unhandled rejection SequelizeHostNotFoundError: Failed to connect to localhostSQLEXPRESS:1433 - getaddrinfo ENOTFOUND localhostSQLEXPRESS at Connection.connection.on.err (C:\Node JS Workspace\db\node_modules\DB\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:97:22) at emitOne (events.js:116:13) at Connection.emit (events.js:211:7) at Connection.socketError (C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connection.js:1016:14) at C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connection.js:861:25 at GetAddrInfoReqWrap.callback (C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connector.js:69:18) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:104:17)

2
  • I think I was // but I am still not able to connect. Unhandled rejection SequelizeHostNotFoundError: Failed to connect to localhost\SQLEXPRESS:1433 - getaddrinfo ENOTFOUND localhost\SQLEXPRESS at Connection.connection.on.err (C:\Node JS
    – Vini
    Commented Jul 19, 2018 at 14:30
  • This should help. stackoverflow.com/questions/45903574/…
    – Chance
    Commented Jul 19, 2018 at 14:51

2 Answers 2

2

This configuration works on my local sql server express:

var sequelize = new Sequelize('db', 'db_user', 'db_pwd', {
    host: 'localhost',
    dialect: 'mssql',
    dialectOptions: {
     options: { instanceName: "sqlexpress" }
   }
});

The trick was in the dialectOptions.instanceName property.

1
  • I was struggle to make it works. And this trick is works for me, "sqlexpress" should be place on instanceName. Wonder why none mention about this.
    – erw13n
    Commented Jan 26 at 2:25
0

Update your host path.

  • Before: localhost\SQLEXPRESS
  • After: localhost\\SQLEXPRESS

Not the answer you're looking for? Browse other questions tagged or ask your own question.