Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Last active February 20, 2018 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjamundx/5cb50cb01e8e1a0c88bb0e2d0c03831d to your computer and use it in GitHub Desktop.
Save xjamundx/5cb50cb01e8e1a0c88bb0e2d0c03831d to your computer and use it in GitHub Desktop.

This seems fine

code:

// @flow
const data = document.body.getAttribute('my-attribute') || '{}'
const result = JSON.parse(data)
document.body.removeAttribute('my-attribute')

result:

Cannot call document.body.getAttribute because property getAttribute is missing in null [1].
Cannot call document.body.removeAttribute because property removeAttribute is missing in null [1].
Found 2 Errors

I'm going to try to fix it

Wrapping in an if, but it only stops the first error.

// @flow
if (document.body) {
    const data = document.body.getAttribute('my-attribute') || '{}'
    const result = JSON.parse(data)
    document.body.removeAttribute('my-attribute')
}

But we still get this one error

Cannot call document.body.removeAttribute because property removeAttribute is missing in null [1].
Found 1 error

The fix is weird?

I had to add the same if again. Why is that?

// @flow
if (document.body) {
  const data = document.body.getAttribute('my-attribute') || '{}'
  const result = JSON.parse(data)
  if (document.body) document.body.removeAttribute('my-attribute')
}

No errors!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment