When running the code below, I encountered an error stating Object is possibly null
in line +localStorage.getItem('PID')
:
newPropID() {
if (localStorage.getItem('PID')) {
localStorage.setItem('PID', String(+localStorage.getItem('PID') + 1));
return +localStorage.getItem('PID');
} else {
localStorage.setItem('PID', '101');
return 101;
}
}
I am looking for a solution to check if there is already a record stored in local storage and add a new one. How can I rectify this issue?