node-mssql icon indicating copy to clipboard operation
node-mssql copied to clipboard

The "config.server" property is required and must be of type string

Open montumodi opened this issue 3 years ago • 10 comments

I am trying to connect to ms sql using below code but I am getting error.

const sql = require("mssql");

(async () => {
  try {
    // make sure that any items are correctly URL encoded in the connection string
    await sql.connect("mssql://sa:reallyStrongPwd123@localhost/dbName");
    const result = await sql.query("select * from mytable");
    console.dir(result);
  } catch (err) {
    console.log(err);
    // ... error checks
  }
})();

Expected behaviour:

Should be able to connect

Actual behaviour:

TypeError: The "config.server" property is required and must be of type string.
    at new Connection (/Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/tedious/lib/connection.js:342:13)
    at /Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/tedious/connection-pool.js:62:23
    at new Promise (<anonymous>)
    at ConnectionPool._poolCreate (/Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/tedious/connection-pool.js:12:12)
    at ConnectionPool._connect (/Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/base/connection-pool.js:358:10)
    at /Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/base/connection-pool.js:330:19
    at new Promise (<anonymous>)
    at ConnectionPool.connect (/Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/base/connection-pool.js:329:12)
    at Object.connect (/Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/node_modules/mssql/lib/global-connection.js:59:27)
    at /Users/abc/Documents/stuff/projects/xyz/eg-xyz-feed-ingestor/src/core/run.js:6:15

Software versions

  • NodeJS: 14.6.1
  • node-mssql: V7.0.0
  • SQL Server: Microsoft SQL Server 2019 (RTM-CU10) (KB5001090) - 15.0.4123.1 (X64) Mar 22 2021 18:10:24 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 20.04.2 LTS) <X64>

montumodi avatar May 07 '21 09:05 montumodi

Can confirm, i have the same issue with v7.0.0, while with v6.2.0 it works fine.

nicosh avatar May 12 '21 12:05 nicosh

Ok - this is probably because the new connection string parser doesn't parse these uri format strings. I'll take a look and update the readme/changelog for now

dhensby avatar May 13 '21 11:05 dhensby

Oh - and to note, the library still has the old connection string parser logic in it (it's just not used) so you can use it to parse your own connection string and then give the resolved config object to the library. I plan on removing it for v8, though.

dhensby avatar May 13 '21 13:05 dhensby

I am seeing the same issue. I also noticed that the aforementioned exception from tedious ("The config.server property is required...") is not caught:

From connection-pool.js:

  const tedious = new tds.Connection(cfg)

tdc.Connection() can throw, should be wrapped in a try-catch, with a call to rejectOnce() inside the catch.

janu75 avatar Jun 04 '21 11:06 janu75

I've opened #1254 to cover @janu75's point that the errors from the new connection do cause the promise to reject.

dhensby avatar Jun 08 '21 18:06 dhensby

Ok - this is probably because the new connection string parser doesn't parse these uri format strings. I'll take a look and update the readme/changelog for now

Where is the documentation for the new string format of connection string? (ex. useUTC=false...)

n0isy avatar Jul 29 '21 08:07 n0isy

Where is the documentation for the new string format of connection string? (ex. useUTC=false...)

There is no new format; simply use a standard connection string for SQL server.

You can see how the connection string values are handled here: https://github.com/tediousjs/node-mssql/blob/v7.2.0/lib/base/connection-pool.js#L92-L273

dhensby avatar Jul 29 '21 10:07 dhensby

in case you encounter this

instead of: mssql://sa:reallyStrongPwd123@localhost/dbName

use Data Source=localhost;Initial Catalog=dbName;User ID=sa;Password=reallyStrongPwd123;trustServerCertificate=true

victor-ca avatar Sep 26 '21 14:09 victor-ca

Note: the types only match config as an object and not a type here https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mssql/index.d.ts#L396-L399

Jolg42 avatar Oct 14 '21 08:10 Jolg42

It would be nice to be able to specify connectionString as an option like in the msnodesqlv8 driver. Currently it's all or nothing when using a connection string - I want to allow a connection string to be used but force certain options (like useUTC).

mohd-akram avatar Jun 30 '22 10:06 mohd-akram