Search code examples
javascriptjsonparsingstoragestringify

Only getting the last entered value when storing a value in sessionStorage.setitem


Okay lets say i enter these two values in my HTML page: 20, 30.

I have these two variabels, i get the value from my HTML page, and it works fine, so BrugerFugt=20, and BrugerTemp=30:

    BrugerFugt = document.getElementById("40").value;
    BrugerTemp = document.getElementById("41").value;

Then i take these two variabels and use setItem.

    sessionStorage.setItem("BF", JSON.stringify(BrugerFugt));
    sessionStorage.setItem("BT", JSON.stringify(BrugerTemp));

At this point if i console.log(JSON.stringify(BrugerFugt)), it's printing 20, as it should.

Now i want to send this to my server, so i call this variable:

  serverConnection.send(JSON.stringify(SetBruger));

I have defined this variable, that i'm sending to my server.

var SetBruger = {
    command: "BrugerIndtastTF",
    brugerT: sessionStorage.getItem("BT"),
    brugerF: sessionStorage.getItem("BF")
}

Then i tryed to print my JSON.stringify(SetBruger), but it's printing the last value i entered on my HTML web, not the new. Let's say before i entered 20 and 30, i entered somthing like 40 and 60. Then when i print my SetBruger after i've entered 20 and 30, i get this print in my termiinal.

test: {"command":"BrugerIndtastTF","brugerT":"\"40\"","brugerF":"\"60\""}

It's like the problem occurs, when i JSON.stringify(SetBruger)?


Solution

  • Your object-properties are set when the object is created.

    var SetBruger = {
        command: "BrugerIndtastTF",
        brugerT: sessionStorage.getItem("BT"), // set when object is created
        brugerF: sessionStorage.getItem("BF") // set when object is created
    }
    

    You can wrap that object with an helper-function that updates the object prior return or replace it with a function that returns an freshly created object with the values from sessionStorage like this:

    function SetBruger() {
      return {
        command: "BrugerIndtastTF",
        brugerT: sessionStorage.getItem("BT"),
        brugerF: sessionStorage.getItem("BF")
      }
    }
    serverConnection.send(JSON.stringify(SetBruger));
    

    To be able to re-use that object and update other properties of it, a helper function that updates the two properties with the content of localstorage would be a good solution.

    var SetBruger = {
        command: "BrugerIndtastTF",
        brugerT: sessionStorage.getItem("BT"), // set when object is created
        brugerF: sessionStorage.getItem("BF") // set when object is created
    }
    
    // Other code that maybe modify the object
    
    function updateBrugerObject() {
        SetBruger.brugerT = sessionStorage.getItem("BT");
        SetBruger.brugerF = sessionStorage.getItem("BF");
    }
    
    updateBrugerObject();
    serverConnection.send(JSON.stringify(SetBruger));
    

    what the best solution for you is, depends on how and where you plan to use the object.