2

I am trying to add a method for when the user gets to the bottom of the window, but typescript is stopping me and saying the object is possibly undefined on window.scrollTop(), height(), and document.height().

I've already tried putting an if statement beforehand and only running the code if it is defined, but I keep getting the error.

mounted() {
    // Does not work because object might be undefined
   $(window).scroll(() => {
     if (
       $(window).scrollTop() + $(window).height() >
       $(document).height() - 100
     ) {
       console.log(this.lastCursor);
     }
   });
 },
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59

1 Answers1

0

Try this:

mounted() {
   this.lastCursor = 0;
   if ($(window) && )
    // Does not work because object might be undefined
   $(window).scroll(() => {
     if (
       $(window).scrollTop() + $(window).height() >
       $(document).height() - 100
     ) {
       console.log(this.lastCursor);
     }
   });
 },

I've tried to eliminate all cases where something could be undefined. If this works, try to identify which variable is undefined.

gatsbyz
  • 1,057
  • 1
  • 10
  • 26