Execute Code card error outside the emulator.
# 🤝help
f
Hello all, I have an execute code card with the instructions to convert date into a readable format from a variable and spit it out for the user to read. The flow works great in the emulator but not outside the emulator, when I try to test the bot in webchat it says that the system has encounter an error. Is there a fox for this?
f
Hey there, Could you share the code and the expanded error message?
f
Hey sure, here you go
f
Hey there, You should be able to see a more in depth error message in the logs, which you can find in the dashboard of your bot
f
Perfect, thank you. So the error
fechacita.toLocalDateString is not a function
occurs because the
toLocalDateString
method is a method of the
Date
object in JavaScript, and it seems that
fechacita
is not a
Date
object. To fix this issue, you need to ensure that
fechacita
is a valid
Date
object. You can change it toa
Date
object like this: https://cdn.discordapp.com/attachments/1219970606636990484/1220342997930217512/image.png?ex=660e97fb&is=65fc22fb&hm=0e72a14cdda52d4d939441ae1f23fc653c06d7534e7f6b099e56ea325cab1739&
f
That seems to be correct yea.
Let me just try and redo this
Alright. I changed it a bit, but not too much.
So we start with a date that can look like this
"2023-02-20T00:00:00.000Z"
.
Copy code
javascript
const options = {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
};
This part creates an object called
options
with three properties: -
year: 'numeric'
- This tells the
toLocaleDateString
method to display the year in a numeric format (e.g., 2023). -
month: 'long'
- This tells the
toLocaleDateString
method to display the month as a full name (e.g., February). -
day: 'numeric'
- This tells the
toLocaleDateString
method to display the day in a numeric format (e.g., 20).
Copy code
javascript
workflow.newDate = workflow.oldDate.toLocaleDateString('en-US', options);
Here I assume that you have the 2 variables oldDate and newDate (see images) -
workflow.oldDate
contains the original date string in the format
"1942-10-17T00:00:00.000Z"
. This is an ISO 8601 formatted date string, which represents October 17, 1942. - The
toLocaleDateString()
method is called on
workflow.oldDate
. This method converts the date to a string representation based on the specified locale and formatting options. - The first argument to
toLocaleDateString()
is the locale, which is set to
'en-US'
(English - United States) in this case. This determines the language and formatting conventions used for the resulting date string. - The second argument is the
options
object defined earlier, which specifies the desired formatting options for the date. - The resulting formatted date string is assigned to
workflow.newDate
. After executing this code,
workflow.newDate
will contain the formatted date string based on the specified options. Given the example date
"1942-10-17T00:00:00.000Z"
, the resulting
workflow.newDate
will be
"October 17, 1942"
. The commented line
//console.log(workflow.newDate);
is used for debugging purposes. If uncommented, it would log the value of
workflow.newDate
to the console, allowing you to see the formatted date string. https://cdn.discordapp.com/attachments/1219970606636990484/1220349360739717271/image.png?ex=660e9de8&is=65fc28e8&hm=b15f2977b05dbb3f0ab5aea3c50e39d728968f2fd903dc9558e3ab7a7b401a14& https://cdn.discordapp.com/attachments/1219970606636990484/1220349361146429453/image.png?ex=660e9de8&is=65fc28e8&hm=4aae6a287e94eec3eb3fed38f181f961e4adc98d0f9ce4012f57b6ac3e37159c&
Copy code
javascript
const options = {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
};

workflow.newDate = workflow.oldDate.toLocaleDateString('en-US', options);
//console.log(workflow.newDate);
I should have used GB and not US, can see that now. You can simply just change US to GB
It will give this instead
17 October 1942
f
Dude thank you soo much, this worked awesome and it was so simple!, BTW I saw a replied from you saying that you have a chatbot that allows you to upload images. I need that function in mu chatbot, how did you do it?
f
That is amazing to hear! You can find the template for it here https://botpress.com/templates/openai-api-template