svelte-google-analytics icon indicating copy to clipboard operation
svelte-google-analytics copied to clipboard

handle when data is undefined (in `addEvent`)

Open hslee2008 opened this issue 7 months ago • 0 comments

The Problem

I think there is one problem but it could be either one.

  1. The documentation is not well written and I didn't know data parameter in addEvent is needed
  2. Or the code isn't designed to handle when data is undefined

Code

addEvent("saw image") this kind of code with the data parameter not specified will result in the following error.

Uncaught TypeError: Cannot read properties of undefined (reading 'send_to')
    at Object.addEvent (functions.js:5:13)
    at Event (event.ts:4:5)
    at HTMLInputElement.change_handler_2 (+page.svelte:2266:33)
    at lifecycle.js:181:40
    at Array.forEach (<anonymous>)
    at HTMLInputElement.bubble (lifecycle.js:181:21)
    at HTMLInputElement.change_handler (Input.svelte:40:5)

This is because the data parameter itself is undefined.

Solution

If documentation needs fixing...

If you don't need any kind of data to be sent, you have to put in {} to avoid the error. This is annoying and unnecessary. I don't think it's the best thing to just enter {} if you could enter nothing.

If the code needs fixing...

I think it's the best thing to check whether data is defined first.

hslee2008 avatar Nov 29 '23 13:11 hslee2008