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();

Thank you very much, Ruslan! But I made the change and it returns the same date!

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

But this is the problem. I need this code to search the JSON for the most recent date within the dates present in the “LASTCOMPRA” variable. And the date 1/1/1970 does not exist in JSON and, therefore, should not be returned.

Below is the JSON so you can see the values ​​of the “ULTIMACOMPRA” variables highlighted in bold:

[
{
“ID ANIMAL”: 225,
“ANIMAL”: “Gandhi”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “Persa”,
“PELAGEM”: “Preta”,
“SEXO ANIMAL”: “Macho”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 42273,
“IDADE ANIMAL”: “8 anos, 1 mês”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “13/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 226,
“ANIMAL”: “Cristal”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “Persa”,
“PELAGEM”: “Tricolor”,
“SEXO ANIMAL”: “Fêmea”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 42273,
“IDADE ANIMAL”: “8 anos, 1 mês”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “12/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 227,
“ANIMAL”: “Brisa”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “Persa”,
“PELAGEM”: “Cinza”,
“SEXO ANIMAL”: “Fêmea”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 41928,
“IDADE ANIMAL”: “”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Óbito”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “15/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 228,
“ANIMAL”: “Zen”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “Persa”,
“PELAGEM”: “Branco e Cinza”,
“SEXO ANIMAL”: “Macho”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 41922,
“IDADE ANIMAL”: “9 anos, 16 dias”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “17/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 229,
“ANIMAL”: “Flora”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “Persa”,
“PELAGEM”: “Tricolor”,
“SEXO ANIMAL”: “Fêmea”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 42273,
“IDADE ANIMAL”: “8 anos, 1 mês”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “14/3/2023”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 230,
“ANIMAL”: “Rhaji”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “SRD”,
“PELAGEM”: “Cinza”,
“SEXO ANIMAL”: “Macho”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 41659,
“IDADE ANIMAL”: “”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Óbito”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “11/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 231,
“ANIMAL”: “Mia”,
“ESPÉCIE”: “Felina”,
“RAÇA”: “SRD”,
“PELAGEM”: “Cinza”,
“SEXO ANIMAL”: “Fêmea”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 41659,
“IDADE ANIMAL”: “”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Óbito”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “18/12/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 232,
“ANIMAL”: “Granola”,
“ESPÉCIE”: “Canina”,
“RAÇA”: “SRD”,
“PELAGEM”: “Branco e Marrom”,
“SEXO ANIMAL”: “Fêmea”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 42125,
“IDADE ANIMAL”: “8 anos, 5 meses, 25 dias”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “10/1/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
},
{
“ID ANIMAL”: 233,
“ANIMAL”: “Wood”,
“ESPÉCIE”: “Canina”,
“RAÇA”: “SRD”,
“PELAGEM”: “Bege”,
“SEXO ANIMAL”: “Macho”,
“ESTERILIZAÇÃO”: “Castrado”,
“DN ANIMAL”: 41821,
“IDADE ANIMAL”: “9 anos, 3 meses, 25 dias”,
“TAG ANIMAL”: “”,
“VIVO/MORTO”: “Vivo”,
“ID TUTOR”: 199,
“TUTOR”: “Ricardo Fontão de Pauli”,
“SEXO TUTOR”: “Masculino”,
“CPF”: “228.226.068-62”,
“RG”: 435605835,
“TAG TUTOR”: “”,
“TELEFONE”: “(48) 99176-0461, (49) 99160-5767, (49) 99922-2019”,
“E-MAIL”: “[email protected]”,
“ENDEREÇO”: “Rua Raimundo Correia 135”,
“BAIRRO”: “Santa Rita”,
“CIDADE”: “Lages”,
“UF”: “SC”,
“CEP”: “88503-090”,
“ORIGEM”: “Indicação”,
“INCLUSÃO”: 42797,
“PRIMEIRA COMPRA”: 43560,
“ULTIMACOMPRA”: “11/1/2021”,
“NPS”: “Promotor”,
“ÚLTIMO ATENDIMENTO”: “”,
“TIPO DE ATENDIMENTO”: “”,
“STATUS”: “”,
“ÚLTIMO ACESSO SIMPLESPET”: 42758.80474537037
}
]

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”

}

]

I copied and pasted your flow into my n8n and it ran your flow perfectly. However, as soon as I connected the IF node to my JSON, I got this error when converting the date!

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

It didn’t work either!

The .toDate() function doesn’t seem to work on my variable!

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?

Unfortunately not!

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?