klaviyo-api-node icon indicating copy to clipboard operation
klaviyo-api-node copied to clipboard

'data.toISOString is not a function'

Open gplusdotgr opened this issue 1 year ago • 6 comments

'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,
                }
              }
            }
          }
        }
      });

gplusdotgr avatar Jan 04 '25 19:01 gplusdotgr

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.

cpcurtis1218 avatar Jan 07 '25 19:01 cpcurtis1218

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

cirkul-marc avatar Jan 15 '25 19:01 cirkul-marc

Agreed, this is a logic bug of the sdk.

gplusdotgr avatar Jan 16 '25 04:01 gplusdotgr

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.

cirkul-marc avatar Jan 16 '25 06:01 cirkul-marc

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.

cpcurtis1218 avatar Jan 16 '25 17:01 cpcurtis1218

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

cirkul-marc avatar Jan 16 '25 18:01 cirkul-marc