Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with date strings #339

Closed
PatrLind opened this issue Oct 29, 2015 · 9 comments · Fixed by #1015
Closed

Problem with date strings #339

PatrLind opened this issue Oct 29, 2015 · 9 comments · Fixed by #1015

Comments

@PatrLind
Copy link

I'm trying send date strings to the database and I'm having some issues.

This is my sample query:
SELECT @p0 AS a
And the parameter is:
'2015-10-31 00:00:00'
I have tried these datatypes with similar result: DateTime, DateTime2, DateTimeOffset

This is the error message:

c:\Klocktornet\DISEServerWebUI\node_modules\any-db-mssql\node_modules\tedious\lib\data-type.js:1514
        var offset = -parameter.value.getTimezoneOffset();
                                      ^
TypeError: undefined is not a function
    at Object.writeParameterData (c:\Klocktornet\DISEServerWebUI\node_modules\any-db-mssql\node_modules\tedious\lib\data-type.js:1514:39)
    at new RpcRequestPayload (c:\Klocktornet\DISEServerWebUI\node_modules\any-db-mssql\node_modules\tedious\lib\rpcrequest-payload.js:95:12)
    at Connection.execSql (c:\Klocktornet\DISEServerWebUI\node_modules\any-db-mssql\node_modules\tedious\lib\connection.js:764:66)
    at _execNextInQueue (c:\Klocktornet\DISEServerWebUI\node_modules\any-db-mssql\index.js:606:11)
    at process._tickCallback (node.js:355:11)

The issue seems to be that the value is converted from a string to an integer in the validate method in data-type.js. The value after the conversion is 1446246000000. It seems to me that this should really be a Date object and not a number.

I can work around this bu sending in real Date objects, but I still think this is an issue that should be solved.

@arthurschreiber
Copy link
Collaborator

Can you do me a favor and check if you also face this issue with version 1.11.5?

And you're right, the parameter handling code is a bit iffy.

@PatrLind
Copy link
Author

Yes, I cannot see any difference with 1.11.5

@arthurschreiber
Copy link
Collaborator

Ok, thanks for letting me know. I just wanted to verify this was not a regression introduced in 1.12/1.13

@styfle
Copy link

styfle commented Mar 10, 2016

I also ran into this problem. The workaround is the following:

var str = "2016-03-10T19:36:16.105Z";
var d = new Date(str);
if (isNaN(d)) {
   // throw error about invalid date string
} else {
    request.addParameter('DateCreated', TYPES.DateTime, d);
}

It would be nice if tedious performed this check when adding a parameter.
For example, something like the following

function addParameter(name, type, value) {
    if (typeof value === 'string' && (type === TYPES.DateTime || type === TYPES.DateTime2)) {
         value = new Date(value);
    }
    // ... remaining code stays the same
}

@jimmont
Copy link

jimmont commented May 23, 2018

THIS ISSUE IS NEARLY 2 YEARS OLD

parameter.value.getTimezoneOffset is not a function this method is being called on a type of object which is not a Date and throwing an error, see (npm 2.6.2) lib/data-types/smalldatetime.js near line 31.

To reproduce add a parameter that is a SmallDateTime.

npm ls
├─┬ tedious@2.6.2

error:
(node:36545) UnhandledPromiseRejectionWarning: TypeError: parameter.value.getTimezoneOffset is not a function
    at Object.writeParameterData (/path/node_modules/tedious/lib/data-types/smalldatetime.js:31:37)
    at new RpcRequestPayload (/path/node_modules/tedious/lib/rpcrequest-payload.js:95:12)
    at Connection.execSql (/path/node_modules/tedious/lib/connection.js:1332:51)
    at Promise (/path/node_modules/tedious-wrapper/index.js:216:28)
    at <anonymous>

@bugg2844
Copy link

This only occurs when options.useUTC is set to false, and the parameter value is something tedious needs to parse e.g. a string containing a date. Tedious uses Date.parse to parse the string, which returns a number of milliseconds, then later tries to call getTimezoneOffset on that value.

@bugg2844
Copy link

Appears to be caused by: f360b09

@bugg2844
Copy link

Looks like https://github.com/tediousjs/tedious/pull/677/files will resolve it.

@arthurschreiber
Copy link
Collaborator

🎉 This issue has been resolved in version 6.6.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants