question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'removeEventListener' of null -Vue issue?

See original GitHub issue

Here is a fiddle to see my problem. https://jsfiddle.net/45t8umsp/ (After adding the keys, go to home and then to /foo. The destroy method fails)

In the event of beforeDestroy I want clean up the instance with:

beforeDestroy() {
    this.placesInstance.destroy();
},

However, I’m not quite sure, it fails with the error:

TypeError: Cannot read property 'removeEventListener' of null
    at EventEmitter.placesInstance.destroy (app.js:229670)
    at VueComponent.beforeDestroy (app.js:229445)
    at callHook (vendor.js:3348)
    at VueComponent.Vue.$destroy (vendor.js:3127)
    at destroy (vendor.js:4605)
    at invokeDestroyHook (vendor.js:6170)
    at VueComponent.patch [as __patch__] (vendor.js:6508)
    at VueComponent.Vue.$destroy (vendor.js:3150)
    at destroy (vendor.js:4605)
    at invokeDestroyHook (vendor.js:6170)

If I’m aspecting the code, I see the error line:

autocompleteContainer.querySelector(".".concat(prefix, "-input")).removeEventListener('input', inputListener);

But I checked in beforeDestory event the input is still there…

What is wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
Haroenvcommented, Nov 27, 2018

Edit test-project

This is the solution: places.js (and autocomplete.js too) modify the DOM to add the container around the input, which makes Vue lose track of the elements, and removes the input, before the beforeDestroy hook is called. The way to go is to make an input with vanilla JS inside a container (so Vue doesn’t control that part at all). The destroying works as expected.

Note however that if you add any listeners yourself, you need to make sure that you remove them in the beforeDestroy hook as well

1reaction
pmochinecommented, Nov 28, 2018

@Haroenv Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get over Cannot read property 'removeEventListener ...
The problem is that someRef.current is mutable, so by the time the cleanup function runs, it may have been set to null. The...
Read more >
[SOLVED] Cannot Read Property 'addEventListener' of Null in ...
To fix the “cannot read property 'addEventListener' of null” error, make sure the selector used the access the element is properly defined.
Read more >
Cannot read property 'addEventListener' of Null in JS
The error "Cannot read property addEventListener of null" occurs when we call the addEventListener() method on a DOM element that doesn't exist.
Read more >
Cannot read property 'removeEventListener' of undefined
Hi, I have the same issue, the error occurs after I do a local query. But the error is not caught by the...
Read more >
Uncaught TypeError cannot read property 'addeventlistener' of ...
In JavaScript, a very common error is the Uncaught TypeError Cannot read property 'addeventlistener' of null. This error occurs when JavaScript is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found