I have been coding my PWA for about a week now and I need some help finding out why the code works completely in Opera, but not in Safari.
How to replicate my issue:
Am I using code that shouldn't be allowed in Safari or something? How do I work around this?
After some debugging on discord, we found that in script.js
, these lines were causing the issue:
if (mealTargetted) {
userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = false;
localStorage.setItem('data', JSON.stringify(userData));
} else {
userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText).addedToShoppingList = true;
localStorage.setItem('data', JSON.stringify(userData));
}
What was happening was that somehow, in safari, e.target.parentNode.parentNode.innerText
had a trailing space compared to the saved localStorage result:
console.log(userData.mealInformation, e.target.parentNode.parentNode.innerText)
This meant that a.name == e.target.parentNode.parentNode.innerText.trim()
was never satisfied, and so userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText)
returns undefined, throwing an undefined is not an object
error, causing the toggle to appear not to change.
What this means was that we had to trim e.target.parentNode.parentNode.innerText
:
if (mealTargetted) {
userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = false;
localStorage.setItem('data', JSON.stringify(userData));
} else {
userData.mealInformation.find(a => a.name == e.target.parentNode.parentNode.innerText.trim()).addedToShoppingList = true;
localStorage.setItem('data', JSON.stringify(userData));
}
So this wasn't a localStorage issue, just a code issue