Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'data.toISOString is not a function' #93

Open
gkatsanos opened this issue Jan 4, 2025 · 6 comments
Open

'data.toISOString is not a function' #93

gkatsanos opened this issue Jan 4, 2025 · 6 comments

Comments

@gkatsanos
Copy link

gkatsanos commented Jan 4, 2025

'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,
                }
              }
            }
          }
        }
      });
@cpcurtis1218
Copy link

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:
Screenshot 2025-01-07 at 12 44 38 PM
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.

@cirkul-marc
Copy link

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.

Image

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:
Image

@gkatsanos
Copy link
Author

Agreed, this is a logic bug of the sdk.

@cirkul-marc
Copy link

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.

@cpcurtis1218
Copy link

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.

@cirkul-marc
Copy link

Thank you @cpcurtis1218, I'm sure future devs who don't use TypeScript (like myself) will be appreciative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants