Node-mssql: When using sql.Int parameter type = parameter.type.validate is not a function

Created on 16 Jun 2015  路  6Comments  路  Source: tediousjs/node-mssql

I'm attempting to execute a query with a number(.Int) parameter type.

My parameter/query looks like this:

var arg = { name: 'id', type: sql.Int, value: 1 };
req.input(arg.name, arg.type, arg.value);

req.query(myQuery, cb);

When it tries to execute that query I get an error in tedious:

.../node_modules/mssql/node_modules/tedious/lib/request.js:144
      value = parameter.type.validate(parameter.value);
                             ^
TypeError: parameter.type.validate is not a function

When I omit the type from the "req.input" it runs fine, it also runs find if I use NVarChar as the type. The SQL column I'm using that parameter to check against in a WHERE clause is a Int, identity column so I feel like sql.Int is the right type for me.

Can anyone explain why I'm getting this error?

bug unconfirmed

Most helpful comment

I can also confirm with @CollinEstes that having two different imported versions of mssql results in this exact same issue.

All 6 comments

Can't reproduce the issue. sql.Int as a parameter for both input and output is covered by this integration test.

I'm actually having this issue with setting any type at the moment, but it may be my design pattern for doing so. I create a mssql parameter ready object in an express middleware function I wrote. (Just pulls the query string/url components and makes mssql ready parameters to sent with the query down the chain.

So before I execute my query I perform the following:

var request = new sql.Request(connection),
  attachInputParametersToSqlRequest = function (parameterObj) {
          request.input(parameterObj.name, parameterObj.type, parameterObj.value);
          // request.input(parameterObj.name, parameterObj.value);
        };

sqlQuery.queryInputParameters.forEach(attachInputParametersToSqlRequest);

My queryInputParameters are defined correctly and when I debug the type is set correctly, ie: sql.NVarChar, or sql.Int. (where sql === mssql)

However it throws the 'parameter.type.validate is not a function' error.

What is interesting is if I hardcode sql.NVarchar which is exactly what parameterObj.type is, it works, but when I use the type I created in another module with a different instance of mssql it fails.

Leaving type off the request.input allows it work properly but I would like to be explicit and provide the type if possible. Will research more later when I have time to come back to it.

Closing due to inactivity.

I can also confirm with @CollinEstes that having two different imported versions of mssql results in this exact same issue.

I ran into this recently after doing a case insensitive global replace which had the intended side effect of changing require('mssql') into require('MSSQL'). I have had other classeswhere I still correctly had require('mssql') and if I then instantiated an instance of the class that required 'mssql' from the class that required 'MSSQL' this problem would arise. Once I corrected the require statement the problem went away.

I can also confirm with @CollinEstes that having two different imported versions of mssql results in this exact same issue.

I found out this happens when using knex and mssql and having two different modules using mssql. Even though it's the same version it fails, I'm having to require mssql in the same module that starts the knex connection and then inject it in the other module. If I do require mssql in the other module that doesn't start knex it fails. This is crazy and took me a lot of time to figure out. My suspicion is that might modify the mssql dependency.

Was this page helpful?
0 / 5 - 0 ratings