Skip to content

TypeError: parameter.value.getTime is not a function - #623

Closed
asage-me wants to merge 4 commits into
tediousjs:masterfrom
asage-me:master
Closed

asage-me wants to merge 4 commits into
tediousjs:masterfrom
asage-me:master

Conversation

@asage-me

Copy link
Copy Markdown

Fixed an error TypeError: parameter.value.getTime is not a function. Parameter needs to be converted to a date to avoid errors.

@asage-me

Copy link
Copy Markdown
Author

There are a few issues fixed with this PR:

tediousjs/node-mssql#377
tediousjs/node-mssql#185
#608

I've created a node that depends on node-mssql and so my node also has these datetime errors.

I also notice that the node on npm is not the same code that's in this repository. How often do you publish to npm?

Comment thread src/data-type.js
writeParameterData: function(buffer, parameter, options) {
if (parameter.value != null) {
let days, dstDiff, milliseconds, seconds, threeHundredthsOfSecond;
var time = new Date(+parameter.value);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the rationale behind this conversion?
If parameter.value is already a Date, the + operator converts it to an integer and the new Date() converts it back to a Date.
If parameter.value is a string that does not contain a number, the + operator produces a NaN.
e.g.: +'2017-10-11' ==> NaN
or: +'Wed Oct 11 2017 19:49:46 GMT+0200' ==> NaN

@chdh

chdh commented Oct 11, 2017

Copy link
Copy Markdown
Collaborator

The root problem of this issue is that sometimes the parameter values are passed through Type.validate() (in Request.validateParameters()) and converted, but sometimes they are not passed through Type.validate() (in BulkLoad.addRow()).

A validation function like Request.validateParameters() should not have the side effect of converting the parameter values and Type.validate() should not return the converted value.

Parameter value conversion should be moved to a separate function which is then called from both Type.validate() and Type.writeParameterData().

Another solution would be to rename validate() to e.g. validateAndConvert() and also call it in BulkLoad.

@asage-me

Copy link
Copy Markdown
Author

So is this something that will be changed?

@chdh

chdh commented Oct 12, 2017 •

Copy link
Copy Markdown
Collaborator

I think the whole conversion logic in the data-type.js module has to be revised.

Conversion to integer is also faulty. parseInt() is used even if the value is already a number and that can produce strange effects, e.g.:

console.log(parseInt(0.000000006)); ==> Output is 6!

Maybe it would be better not to convert at all and just throw a TypeError in writeParameterData() when the data type does not match? Why is it important to automatically convert a string to a Date?

@asage-me

asage-me commented Oct 12, 2017 •

Copy link
Copy Markdown
Author

I don't think that it's important to convert, the values should be passed in the correct format to begin with. However, I've seen this issue a couple places and that is the fix. It was initially posted here tediousjs/node-mssql#185 and line 1413 of data-type.js has the same code to convert DATETIME2 time = new Date(+parameter.value); I'm not a professional programmer, so if there is something that needs to be addressed in general with data-types.js then I would be glad to see it put in place. Maybe add some logic to convert and if it fails then send an error back. Currently the error is not trapped and crashed the whole process.

@asage-me

Copy link
Copy Markdown
Author

After some more testing I have found that the problem is the node-mssql node converts the datetime into a number. See here starting on line 166 https://github.com/patriksimek/node-mssql/blob/master/lib/datatypes.js I will raise an issue there as well as this seems incorrect.

@asage-me

Copy link
Copy Markdown
Author

tediousjs/node-mssql#546 Issue raised here

@chdh

chdh commented Oct 13, 2017

Copy link
Copy Markdown
Collaborator

I don't agree. Starting at line 166 in https://github.com/patriksimek/node-mssql/blob/master/lib/datatypes.js, Date values are not converted to number but to a string in the format N'yyyy-mm-dd hh:mm:ss'.

@asage-me

Copy link
Copy Markdown
Author

You are right, I wasn't looking at it very hard. It must get converted somewhere else to a number because when it gets to the section of this file it is indeed a number type. Either way, neither of those types are a date and in node-mssql there is no possible way to send a date type so it fails.

@chdh

chdh commented Oct 14, 2017

Copy link
Copy Markdown
Collaborator

@arthurschreiber @v-suhame what do you think about automatic value conversion for SQL statement parameters and BulkLoad field values? Should Tedious try to convert the data type of values that do not match, or just throw a TypeError?

Examples:

  • Convert a string to an integer.
  • Round a float value to an integer.
  • Convert a string to a Date.

The current implementation converts values for SQL statement parameters but not for BulkLoad field values. There are some errors and discrepancies in data-type.js that should be fixed. How can we proceed?

@arthurschreiber

Copy link
Copy Markdown
Collaborator

Maybe it would be better not to convert at all and just throw a TypeError in writeParameterData() when the data type does not match? Why is it important to automatically convert a string to a Date?

Yeah, so I totally think we should not be doing any conversion between datatypes. 👍

I don't think that it's important to convert, the values should be passed in the correct format to begin with.

Yup, again, this is the way to go. 😅


I think long-term, here's what we need to do (and what probably is the only sane approach to this):

tedious should provide a system to plug in custom data-type support modules. Each module would handle parsing of TDS data values into JavaScript objects and converting JavaScript objects back into TDS data values.

Out-of-the box, tedious should only support TDS data types that can be converted to equivalent JavaScript data types without a loss of precision or other information. E.g. we would support "basic" data types like varchar, char, binary, varbinary, int, etc.

If a user of tedious needs to work with other data types that can't be represented in plain JavaScript (like bigint), they'd have to provide a data-type module. These could be either write by them (to satisfy their needs) or written by us (to provide a base set).

Let's use the bigint example again: There's currently a bunch of arbitrary precision integer libraries available for JavaScript (https://www.npmjs.com/browse/keyword/biginteger). Whichever one we pick, someone will be unhappy because we didn't use the library they use in their application. By having a pluggable system, we can provide a base set of types for "popular" libraries, and if they need something special, they can go and build their own.

This would also solve the issue of providing conversion logic - if someone believes they really need to be able to convert String objects to DateTime values, they should be able to hook up their own data-type handler and do whatever they want. 😄

@chdh What do you think?

@chdh

chdh commented Oct 19, 2017

Copy link
Copy Markdown
Collaborator

@arthurschreiber A plug-in architecture would be a very good and flexible solution. But in the short term, we should fix the problems with the "basic" data types. This would be a small change.

@arthurschreiber

Copy link
Copy Markdown
Collaborator

@chdh You're right, we need a fix in the interim and can't leave this broken. As you've worked on the BulkLoad code before, what would be you preferred way to fix this?

@chdh

chdh commented Oct 19, 2017 •

Copy link
Copy Markdown
Collaborator

@arthurschreiber I would just clean up the existing validate() and writeParameterData() functions for each data type in data-type.js and add validate() calls inBulkLoad.

If we want backward compatibility, so that the PR can already be included in a Tedious 2.x release, I would add convert() functions for each data type and call these in Request.validateParameters() just before the validate() calls. In this case, validateParameters() should be renamed to convertAndValidateParameters(). We could add an option to enable/disable automatic data type conversion of SQL statement parameters. For Tedious 2.x, the option would be enabled by default. For 3.x it would be disabled.

@niwsa

niwsa commented Nov 4, 2017

Copy link
Copy Markdown

Any update on the fix? I am facing the same issue.

@arthurschreiber

Copy link
Copy Markdown
Collaborator

Hey there,

I've been spending a lot of time going through the internals of tedious and it's data type system. There's a lot of weird code in there (as pointed out by @chdh).

I'll be opening a PR shortly that will fix this issue here by throwing a more descriptive error message when an unexpected type is passed to the different writeParameterData functions for different data types. As that is not a breaking change and will clearly signal to our users why they're seeing an error (which is much better than just throwing some obscure javascript error).

We'll have to find a better method of handling types long term, but that's a separate discussion from the error and fix proposed here. 😞

Thank you for opening this PR and getting the discussion started. ❤️ I'm really sorry this has taken a long time to start moving to a solution 😞.

I really appreciate the time you spent and hope the solution I'm suggesting above is satisfactory. If not, please let us know!

@asage-me

asage-me commented Jan 6, 2018

Copy link
Copy Markdown
Author

Any progress is good, I'll take it!

@Shaffuan

Copy link
Copy Markdown

Any Progress Guy???

@esterkjuicepress

Copy link
Copy Markdown

Was this fix ever merged? I still have issues with bulk inserting DateTime types

TypeError: parameter.value.getTime is not a function
at Object.writeParameterData (\node_modules\tedious\lib\data-types\datetime.js:30:44)

@stegel

stegel commented Jun 3, 2019

Copy link
Copy Markdown

was this ever resolved?

@thbl

thbl commented Dec 8, 2019

Copy link
Copy Markdown

+1

@MichaelSun90

Copy link
Copy Markdown
Contributor

@arthurschreiber, I had a different PR #907 that holds a similar change, and it already updated to the latest master. I am not sure whether that we should close this PR and get the other PR merged in?

@arthurschreiber

Copy link
Copy Markdown
Collaborator

I think with the changes in #1065 and validateBulkLoadParameters set to true this is no longer necessary? Please feel free to reopen if you think I'm mistaken. 🙇‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants