'data.toISOString is not a function'
'TypeError: data.toISOString is not a function
stuck in this for hours.. any help would be appreciated!
const date = new Date().toISOString();
const response = await eventsApi.createEvent({
data: {
type: "event",
attributes: {
properties: {
hotelId,
checkInDate,
checkOutDate,
bookingId,
firstName,
},
time: date,
uniqueId:"696b3941-6ec8-42cc-854f-1f90be51d8de",
metric : {
"data": {
"type": "metric",
"attributes": {
"name": "Placed Order"
}
}
},
profile: {
data: {
type: 'profile',
attributes: {
email: recipientEmail,
}
}
}
}
}
});
Hi there, is is possible that the error is being thrown from another reference to toISOString() than you've shared here? When I test that line is working for me:
I also want to note that it's not necessary to include the "time" in the event data body - the API will automatically set the time to the moment the event was received by the API and so setting the "time" like this is not required.
Also getting this error. This is not a problem with @gkatsanos calling const date = new Date().toISOString();, it appears to be this library trying to call toISOString() on something that is already a string.
Example code that generates the above error:
return Klaviyo.Events.createEvent({
data: {
type: 'event',
attributes: {
profile: {
data: {
type: 'profile',
attributes: {
email: email,
},
},
},
metric: {
data: {
type: 'metric',
attributes: {
name: eventName,
}
}
},
properties: eventProperties,
time: '2022-11-08T00:00:00+00:00', // taken directly from docs, see attached screenshot
},
},
});
Format specified in docs:
Agreed, this is a logic bug of the sdk.
As a follow-up, I tried passing a JS Date instead of an ISO string and the problem went away.
Unfortunately this behaviour doesn't appear to be documented anywhere. Would be great to get it fixed so ISO strings are accepted as well, like they used to be in older versions of this library.
Thank you for the additional details - the TypeScript type of that field is Date, not string (shown here).
We will upgrade this so ISO strings are accepted as well.
Thank you @cpcurtis1218, I'm sure future devs who don't use TypeScript (like myself) will be appreciative.