Updating data through SessionStorage.set() method isn't reflecting changes

I am facing an issue with the code below, as the sessionstate does not seem to update properly. The initial result shows 3 social security numbers, but after attempting to update it with 100 new numbers, the count remains the same. Any insights on why this might be happening?

 window.onbeforeunload = () => {
        const ssnLs = $("#members-list-table")
            .DataTable()
            .rows({ filter: "applied" })
            .data()
            .map((item: any) => {
                return item.socialSecurityNumber;
            })
            .toArray();
        
        //added this line for testing - to see what value was here before I update
        var localStorageWithMembersArray1 = JSON.parse(localStorage.getItem("socialnumbers"));
       

        sessionStorage.setItem("socialnumbers", JSON.stringify(ssnLs));
        
        //added this line for testing - to see what value is here after I update
        var localStorageWithMembersArray = JSON.parse(localStorage.getItem("socialnumbers"));

    };

Answer №1

Perhaps your issue lies in the choice of storage you are using based on the provided example.

You seem to be writing to sessionStorage but attempting to retrieve from localStorage. Keep in mind that data stored in sessionStorage will not automatically transfer over to localStorage, and vice versa.

To resolve this, consider modifying your final line to:

var localStorageWithMembersArray = JSON.parse(sessionStorage.getItem("socialnumbers"));

By doing so, you should observe the desired update.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Retrieving an element from an array may result in a value of

While going through an array of objects in my Angular component class, I faced a strange issue where the properties kept showing up as undefined. The function responsible for this behavior looks like this: upload(): void { const { fileHandles, related ...

Broadcasting TypeScript Data Structures via Socket.IO

In my Typescript code, I have created a basic User class as shown below: export class User { constructor(public id: string); } When I emit a message from my socket.io server, I include an instance of the User class like this: var user = new User(&ap ...

Vanilla JavaScript error: Unable to access property

I am working on implementing a header with a logo and navigation that includes a menu toggle link for smaller viewports. My goal is to achieve this using Vanilla JS instead of jQuery. However, when I click on the menu toggle link, I encounter the followin ...

I encountered an error while running a basic express app with ts-node-dev: Invalid argument: A non-string value was provided to `ts.resolveTypeReferenceDirective`

Recently, I started diving into Typescript and Express. Trying to set up a basic Express app using ts-node-dev, I encountered the following error: > ./node_modules/.bin/ts-node-dev src/index.ts 16:07:40 [I ...

Most effective method for converting a table of data to TypeScript

Searching for an effective method to map a table of enum (or interface) data to the correct location. https://i.sstatic.net/5hF2q.png For instance, Smoke Sensor - Push Button can only be linked to SS - PI SYMBOL and Smoke Sensor - PushButton can only be ...

Add a class to a button in an Angular btn-group if a specific string is found within an

I am currently working on a project where I have multiple buttons that need to toggle an active class when selected in order to change their color. Below is a snippet of what I have: In the array called 'selected', I have: this.selected = [&ap ...

Checking for unnecessary properties in Typescript with vue-i18n

Let's consider two different languages represented in JSON format: jp.json { "hello": "こんにちは" } ge.json { "hello": "hallo", "world": "welt" } Now, we are going to com ...

Error: Unable to modify a property that is marked as read-only on object '#<Object>' in Redux Toolkit slice for Firebase Storage in React Native

Hey there! I've been working on setting my downloadUrl after uploading to firebase storage using Redux Toolkit, but I'm facing some challenges. While I have a workaround, I'd prefer to do it the right way. Unfortunately, I can't seem to ...

Tips for modifying HTML template code within a Typescript document using Atom

There appears to be a current trend in Angular development where the template code is embedded within the Angular component, usually found in a Typescript or Javascript file. However, when attempting this approach, I noticed that I am missing html syntax ...

How can I generate pure JavaScript, without using Typescript modules?

Take this scenario as an example ... index.ts import { x } from "./other-funcs"; function y() { alert("test"); } x(y); other-funcs.ts import { z } from "some-module"; export function x(callback: () => void): void { z(); callback(); } ...

What is the best way to combine two messages in the Global Message Service within an Angular application?

In a particular scenario, I am faced with the need to combine two error messages - one from a backend response and the other being a localized and translated text. The code snippet in question is as follows: this.globalMessageService.add( { key: ...

The Angular ngx-color Color Picker is missing the option to customize the width of the color picker

I am currently utilizing the ngx-color Color Picker: https://www.npmjs.com/package/ngx-color#material This is the section where I am implementing the Color Picker and trying to specify the width <div> <div class="flex-title"> ...

Unlock the power of the multiselect dropdown component in MUI with direct access

I'm intrigued by the multi-select capabilities of the MUI Select component when using the multiple=true option. However, rather than having a traditional dropdown menu, I want to integrate the selection options directly into a div on the page. I prefe ...

Use the ngFor directive to iterate over the most recently created array from the parent ng

I am looking to link material tabs with ngFor to generate arrays for child ngFor. Let's start from the beginning: <mat-tab-group> <mat-tab *ngFor="let tab of asyncTabs "> <ng-template mat-tab-label>{{tab.label}}</ng-template ...

Tips for utilizing ion-img within an Ionic 3 application?

I am currently developing an app using Ionic 3 that includes a large number of images spread across different pages. Initially, I used the default HTML image tag to display these images. However, this approach caused lag in the app's performance and a ...

Building a hierarchical tree structure using arrays and objects with Lodash Js

I am attempting to create a tree-like structure using Lodash for arrays and objects. I have two arrays, one for categories and the other for products, both with a common key. The goal is to organize them into a tree structure using string indexing. let ca ...

Trigger the Angular Dragula DropModel Event exclusively from left to right direction

In my application, I have set up two columns using dragula where I can easily drag and drop elements. <div class="taskboard-cards" [dragula]='"task-group"' [(dragulaModel)]="format"> <div class="tas ...

Tips for typing a destructured object key in TypeScript

Assuming I have a query parameter from the router in my Next.js app const { query: { id }, } = useRouter(); The value of { id } is currently string | string[] | undefined. I want to send it as a parameter to another function, and I am certain that ...

Incorporate personalized elements into your @react-three/fiber environment

My dilemma lies in the fact that I am trying to incorporate my custom components into my scene. However, these custom components are defined in a separate file. When I attempt to define my custom component, I encounter an error message stating: Cannot find ...

Determine the full location information with the help of Google Maps SDK without the need

My current challenge involves dealing with a list of unformatted and incorrectly written addresses. I am seeking a way to iterate through these flawed strings and generate more organized and accurate addresses using one of the many Google Maps SDKs availa ...