JavaScript on Code

Error in JavaScript on Code node

Hello everyone, everything good?!

I have a problem with a Code node on n8n and I need help.

This is a flow that I am still building that will synchronize my customers’ Whatsapp Business tags daily based on the date of the last purchase. If the last purchase was made less than 30 days ago, they will be considered ACTIVE and, otherwise, they will be considered INACTIVE.

I’m having a problem with the code node that runs JavaScript code to evaluate the most recent purchase date in a previous JSON. The dates are in the ULTIMACOMPRA variable in JSON and this code selects the last purchase in JSON and then continues the flow.

However, the code is returning an invalid date, which is not even present in the spreadsheet. In fact, on 01/01/1970 I wasn’t even born yet and, therefore, I couldn’t have attended the service! :sweat_smile:

Anyway, I need to find out what the error is in this code. Is it the first date in the timestamp? How could I get around this error?

  • n8n version: 1.22.6
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: MacOS

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I already included the information in the post. Thanks!

Hi, probably you should change json = items to json = $input.all();

Your new Date(0) = 01/01/1970

Would It be a better way to use IF node? Something like that:

The problem is that in this case I would already have to map all the variables to be compared and this varies in each case. Note that this JSON that I sent above is a file for each owner’s pet. There are owners who will have 1, 2 or more pets. This JSON is an example that was extracted from previous nodes, but will vary for each client.

I am running this same JavaScript code on Uchat, with small adaptations. However, I brought it to n8n to be able to perform daily stream triggers, whereas this is not possible on Uchat!

In Uchat, the JavaScript code is working perfectly and returns exactly the customer’s most recent purchase date based on the information present in the Google Sheets spreadsheet on the previous node. However, when redoing this flow in n8n I can’t get it to work the same way!

The difference is that in Uchat I am able to filter only the ULTIMACOMPRA variable to pass on in the JSON. Perhaps this is also a factor that is generating this error! Therefore, UCHAT is returning the JSON below:

[
{
“ULTIMACOMPRA”: “18/12/2021”
},
{
“ULTIMACOMPRA”: “17/12/2021”
},
{
“ULTIMACOMPRA”: “15/12/2021”
},
{
“ULTIMACOMPRA”: “14/03/2023”
},
{
“ULTIMACOMPRA”: “13/12/2021”
},
{
“ULTIMACOMPRA”: “12/12/2021”
},
{
“ULTIMACOMPRA”: “11/12/2021”
},
{
“ULTIMACOMPRA”: “11/01/2021”
},
{
“ULTIMACOMPRA”: “10/01/2021”
}
]

In n8n, we consider using the Code node as a last resort. If you need to filter solely by ‘ULTIMACOMPRA’: ‘10/01/2021’ and compare it to something else, I suggest using the IF node. In the workflow example I posted above, you can find a simple guide on how to do this. The IF node will process all your items.

I didn’t work for myself!

…even activating the Less Strict Type Validation option

What error in a first condition?
You should put there
{{ $json.ULTIMACOMPRA.toDate().minus(day=30).format("dd-MM-yyyy") }}

And here is json I’am using
[

{

“CEP”: “88503-090”,

“ORIGEM”: “Indicação”,

“INCLUSÃO”: 42797,

“PRIMEIRA COMPRA”: 43560,

“ULTIMACOMPRA”: “10/1/2021”

},

{

“CEP”: “88503-090”,

“ORIGEM”: “Indicação”,

“INCLUSÃO”: 42797,

“PRIMEIRA COMPRA”: 43560,

“ULTIMACOMPRA”: “11/1/2021”

}

]

Use this
{{ $json.[“ULTIMACOMPRA”].toDate().minus(day=30).format(“dd-MM-yyyy”) }}

So {{ $json.ULTIMACOMPRA }} shows the date as I can see. Start adding functions one by one and see which one causing a problem.
Does {{ $json.ULTIMACOMPRA.toDate() }} works?

Probably because when it tried to convert, the date was already in the wrong format. To correctly convert the date, it should be in MM/dd/YYYY format. However, you provided it in dd/MM/yyyy format. And of course, on Earth, there is no 13th month. :slight_smile:

Here in Brazil we use the dd/MM/yyyy format, so the dates are in this format. I’ll try to format! Do you recommend using the Date&Time node or the Code node?

You can read more about date formats in n8n here https://docs.n8n.io/code/luxon/#if-you-have-a-date-in-a-supported-standard-technical-format

@ rfdepauli Try this solution

1 Like

Thank you very much, Ruslan_Yanyshyn! I made a few adjustments and it worked perfectly for me! Thank you for all your help and patience!

1 Like

Now that I realize I left my credentials exposed here. I will generate a new API Token in Z-API. Therefore, perhaps the flow will be interrupted for anyone who comes to follow our conversation from now on!

Thank you very much again, Ruslan!

1 Like

Welcome Ricardo, I suppose you don’t need Start Node in your workflow since you have Cron node.

1 Like

Yes! I just left the Star node to perform the tests!