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!