Skip to content

Problem with date strings #339

Closed

Description

@PatrLind

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.

Activity

  1. arthurschreiber commented on Oct 29, 2015

    @arthurschreiber
    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.

  2. PatrLind commented on Oct 29, 2015

    @PatrLind
    Author

    Yes, I cannot see any difference with 1.11.5

  3. arthurschreiber commented on Oct 29, 2015

    @arthurschreiber
    Collaborator

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

  4. styfle commented on Mar 10, 2016

    @styfle

    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
    }
  5. jimmont commented on May 23, 2018

    @jimmont

    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
    ├─┬ [email protected]
    
    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>
    
  6. bugg2844 commented on May 24, 2019

    @bugg2844

    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.

  7. bugg2844 commented on May 24, 2019

    @bugg2844

    Appears to be caused by: f360b09

  8. bugg2844 commented on May 24, 2019

    @bugg2844
  9. arthurschreiber commented on Dec 15, 2019

    @arthurschreiber
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions