Function node - getFullYear error

Hello:

I have a flow that selects data from a table and inserts the data into a backup table in another database, This has been working just fine for over 6 months. After I select the data from the source, a function node converts the data into a SQL statement to insert into the target, Today I started getting an error in the function "getFullYear" where I am parsing datetime data from the source and building a date string. The error that I get is "m.created.getFullYear() is not a function".

The code in the function node is:

var m = msg.payload
var t =""

for(x=0;x<m.length;x++){
    t = "INSERT INTO events SET row_id="+m[x].row_id+", source='"+m[x].source+"',name =' "+m[x].name
    t=t+ "', displayName='"+m[x].displayName+"', value='"+m[x].value+"', unit='"+m[x].unit+"', deviceId="+m[x].deviceId
    t = t+", hubId="+m[x].hubId+", locationId="+m[x].locationId+", installedAppId="+m[x].installedAppId
    t= t+ ", descriptionText='"+m[x].descriptionText+"', created="
    var year=m[x].created.getFullYear() **<= Error here**
    var month = m[x].created.getMonth()+1
    var date = m[x].created.getDate()
    var hour = m[x].created.getHours()
    var minute = m[x].created.getMinutes()
    var seconds = m[x].created.getSeconds()
    var sqlDate = year+"-"+month+"-"+date+" "+hour+":"+minute+":"+seconds
    t=t+"STR_TO_DATE('"+sqlDate+"','%Y-%m-%d %H:%i:%s')"
    
    node.send({topic:t})
    //x=(m.length)+1
}

Any idea what is going on? NR version is 1.2.7 on RPi4. Thanks for your help.

It is most likely that msg.payload.[...].created is not a JavaScript date object - almost certain in fact since you pulled the data from another db.

You need to look at the format of that date/time string (or maybe number) to make sure that it can be parsed to a JavaScript Date.

Have you updated any contrib nodes or node-red or nodejs or the database table structure?
Also, what SQL DB (e.g. mySQL, MSSQL, sqllite?) and what SQL contrib node are you using?

On you fixing what you have

To understand what is happening, add the following on the first line after the for(...) {

node.warn(`type of created: ${typeof m[x].created}, value of created: ${m[x].created}`);

What type and what value to you see?

I have not updated node-red or node.js recently (at least since last week when this was working). Also, I have not changed the table structure of this particular table.

I'm using mySQL and node-red-node-mysql (v 0.1.4). I did update this node from v0.1.2 yesterday :frowning:

I think we have a winner :wink:

If you are able to test, please re-install the v0.1.2 and restart node-red. Does it fix your issue?

To install older version...

  • cd into your node-red folder (usually cd ~/.node-red or cd c:\users\USERNAME\.node-red
  • npm install node-red-node-mysql@0.1.2
1 Like

Don't forget to restart node-red after doing a command line install.

1 Like

Here is what I get:

type of created: string, value of created: 2021-03-13T10:30:00.000Z

I tried converting this to java date/time but still get an error on .getFullYear

var dateJS = new date(m.created)
var dateYear = dateJS.getFullYear()

Ugh - I use a pre-bulit image of Homebridge so will have to make sure that I don't mess that up!! Will try that.

you dont have to - you can work around this.

var dateJS = new Date(m[x].created)
var dateYear = dateJS.getFullYear()

YOUR ERROR: it is new Date - NOT - new date (case sensitive)

1 Like

THANK YOU! That seems to work. Do you think I should create an issue on Github for this or would change be by design?

A minor release would not intentionally introduce a breaking change, so yes, I think you should create an issue.

1 Like

@colin @Steve-Mcl

Very strange - I reverted to 0.1.2 on a test instance of NR and I still get the same error. I did restart NR and confirmed that I the version of node-red-node-mysql is 0.1.2. I was going to create and issue but now I'm not sure what is causing this.

What exactly is it that has changed?

I'm trying to remember - but the basic platform has not changed. I updated the mySQL node and some other nodes that were showing updates (unfortunately I can't recall which ones).

I meant what is it that the SQL node does that it is now different? The code you posted suggested that it used to return a Date object, but now it returns a String.

I think this comes from a naive fix from this thread - Splitting array - from MySQL query
I have now tried to do a better object copy in version 0.1.5 so hopefully anything is now preserved, though my simplistic testing always seems to return strings anyway.

1 Like

Yes - that's what I was hoping to see when I reverted to 0.1.2 but it is typed as "string" now. I put in a debug to get the type and this is what is shows:

type of created: string, value of created: 2020-06-01T10:15:00.000Z

I would agree that it seemed to be returning a date type before (or at least the function node was able to work with it like a date object), but now I can't get it to work even with 0.1.2

Unfortunately, it's not working with 0.1.5 either. My table structure is:

The code in the function node that I'm using to debug is:

Similar code was working last week as I have a scheduled flow that copies data from one source to a backup and it stopped working yesterday. I have worked around this by converting the date returned from the query into a java date (using new Date()). Please let me know if you need any other information. Thanks.

EDIT: This seems that node.js was returning dates as javascript dates. Could something have changed there? (see response # 16).
https://stackoverflow.com/questions/32100434/mysql-returns-full-datetime-string-on-select-query-when-column-type-is-date

In particular from that post
"this feature can be turned off by setting dateStrings to true in the connections:"

So that suggests that maybe something has changed and that flag is now set to true where it was set to false (or maybe defaulting to false) previously.

1 Like

good catch & very likely.

Maybe a feature request for adding that option to the mysql connection config node is in order.

Another option would be the ability to add any additional flags you desire (that the mysql lib supports) via a JSON only typedInput that are then passed to the connection?

1 Like

the connection we use is here

so no dateStrings setting there...