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!
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
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:
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.
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.
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?
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!