private currentStorage = null;
setItem(key: string, value: string) {
this.currentStorage.setItem(key, value);
}
When I run my code (the above snippet is the only one that contains the setItem) I get the following error:
ERROR TypeError: Cannot read properties of null (reading 'setItem')
at SessionService.setItem (session.service.ts:53:23)
at authentication.service.ts:58:37
at map.js:7:1
at OperatorSubscriber._next (OperatorSubscriber.js:13:1)
at OperatorSubscriber.next (Subscriber.js:31:1)
at map.js:7:1
at OperatorSubscriber._next (OperatorSubscriber.js:13:1)
at OperatorSubscriber.next (Subscriber.js:31:1)
at filter.js:6:50
at OperatorSubscriber._next (OperatorSubscriber.js:13:1)
I've tried many ways to define the currentStorage but the error is the same or sometimes says "properties of undefined" instead of "properties of null".
Can anyone explain what is happening?
this was all could provide based on question
setItem(key: string, value: string) {
if(this.currentStorage){
this.currentStorage.setItem(key, value);
}
else{
...
//could do nothing here or create the storage
this.currentStorage = ...
this.currentStorage.setItem(key, value);
}
}
but if you are looking for actual change needed for session storage
setItem(key: string, value: string) : void {
sessionStorage.setItem(key, JSON.stringify(value));
}
/** get an item from session storage */
getItem(string: key) : any | undefined{
let value = sessionStorage.getItem(key);
if(value){
return JSON.parse(value);
}
return undefined;
}