Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript localStorage.setItem(); is considered undefined

~prop

Coder
Hello.
I have a problem with my code in JS. I am trying to make new user by reading the input prompt (first function) and saving it into the local storage (second function) but it doesn't seem to work, once I open the project in live server by Visual Studio Code and try to make new user, it shows error in edge console and says that the "localStorage.setItem(...)" is undefined. I tried to change something or do the same thing in different ways but it still won't work out. Please help me. (There is "var" used in variable making, it is a school project and they thought us to use this but can you also say how to make this with "let"? )

Thanks for help, here is the code of JS, if needed, I will post the other files linked to it like HTML to find the solution.

JavaScript:
function newUser() {
    var new_username = document.getElementById("username-field").value;
    var new_password = document.getElementById("password-field").value;
    if ((new_username == "" || new_password == "") || (new_username == "" && new_password == "")) {
        alert("Neesi ievadījis vajadzīgos datus!");
    }
    else {
        localStorage.setItem("username", new_username).value;
        localStorage.setItem("password", new_password).value;
        alert("Esi izveidojis jaunu lietotāju!");
    }
}

function checkUser() {
    var username = document.getElementById("username-field").value;
    var password = document.getElementById("password-field").value;
    var user = document.getElementById("username").value = localStorage.getItem("username");
    var pass = document.getElementById("password").value = localStorage.getItem("password");
    if (user == username) {
        if (pass == password) {
            console.log("Dati patiesi!")
        window.location.replace("program.html");
        }
    }
    else {
        document.getElementById("notification").textContent = "Nepareizs lietotājvārds un/vai parole!";
    }
}
 
Solution
D
Hi again. I looked into the code again and noticed that I typed .value after getting info from storage, I removed it, also changed 17th and 18th line and so far it works. Thanks for your help, I was stuck there for 2 days already.
Oh, I completely missed that .value ... Sloppy reading. But I blame JS for giving such a misleading message 🙄 Good that you found it.
One more question, would it be easy to make this project not work by variables (var) but (let) instead?
Possibly. Generally speaking, you cannot just replace var by let everywhere. You have to really think about the scope of your variables. If they are local to a {..} block, use let. Otherwise, use var. Or just be lazy...
Hello.
I have a problem with my code in JS. I am trying to make new user by reading the input prompt (first function) and saving it into the local storage (second function) but it doesn't seem to work, once I open the project in live server by Visual Studio Code and try to make new user, it shows error in edge console and says that the "localStorage.setItem(...)" is undefined. I tried to change something or do the same thing in different ways but it still won't work out. Please help me. (There is "var" used in variable making, it is a school project and they thought us to use this but can you also say how to make this with "let"? )

Thanks for help, here is the code of JS, if needed, I will post the other files linked to it like HTML to find the solution.

JavaScript:
function newUser() {
    var new_username = document.getElementById("username-field").value;
    var new_password = document.getElementById("password-field").value;
    if ((new_username == "" || new_password == "") || (new_username == "" && new_password == "")) {
        alert("Neesi ievadījis vajadzīgos datus!");
    }
    else {
        localStorage.setItem("username", new_username).value;
        localStorage.setItem("password", new_password).value;
        alert("Esi izveidojis jaunu lietotāju!");
    }
}

function checkUser() {
    var username = document.getElementById("username-field").value;
    var password = document.getElementById("password-field").value;
    var user = document.getElementById("username").value = localStorage.getItem("username");
    var pass = document.getElementById("password").value = localStorage.getItem("password");
    if (user == username) {
        if (pass == password) {
            console.log("Dati patiesi!")
        window.location.replace("program.html");
        }
    }
    else {
        document.getElementById("notification").textContent = "Nepareizs lietotājvārds un/vai parole!";
    }
}
Hi there,
Part of your issue is here
1670942032413.png
Can you walk me through your thought process for this method?
 
Hi there,
Part of your issue is here
View attachment 1857
Can you walk me through your thought process for this method?
Hi, thanks for replying. My thought was to get the registered new account details from local storage that i set in newUser() and define it as a variable, then comparing it to a variable that was defined from entered info in login page by using checkUser(). I tried something different a couple of times but it never worked.
 
When I try to register new user by function, it shows this output in console, also storage section is not showing any data stored, before some changes in code it showed storage but the code did not work anyways.
 
Hi, here are all the files attached to this project, program.html is not yet ready because of me being stuck with this message.
 

Attachments

  • Screenshot (25).png
    Screenshot (25).png
    211.4 KB · Views: 3
  • Screenshot (26).png
    Screenshot (26).png
    206 KB · Views: 3
  • Screenshot (27).png
    Screenshot (27).png
    148.5 KB · Views: 2
  • Screenshot (28).png
    Screenshot (28).png
    128.5 KB · Views: 3
Hi, here are all the files attached to this project, program.html is not yet ready because of me being stuck with this message.
HI there,
Would it be possible for you to copy your code and paste it on here? Please use the [ CODE ] [ /CODE ] attributes to separate it
 
Hi again. I looked into the code again and noticed that I typed .value after getting info from storage, I removed it, also changed 17th and 18th line and so far it works. Thanks for your help, I was stuck there for 2 days already.

One more question, would it be easy to make this project not work by variables (var) but (let) instead?
HI there,
Would it be possible for you to copy your code and paste it on here? Please use the [ CODE ] [ /CODE ] attributes to separate it
 
Hi again. I looked into the code again and noticed that I typed .value after getting info from storage, I removed it, also changed 17th and 18th line and so far it works. Thanks for your help, I was stuck there for 2 days already.
Oh, I completely missed that .value ... Sloppy reading. But I blame JS for giving such a misleading message 🙄 Good that you found it.
One more question, would it be easy to make this project not work by variables (var) but (let) instead?
Possibly. Generally speaking, you cannot just replace var by let everywhere. You have to really think about the scope of your variables. If they are local to a {..} block, use let. Otherwise, use var. Or just be lazy like me and use var everywhere 😁

BTW - If you are ever asked for code again, please do not post screenshots !
 
Solution
Thanks for reply, i like that var is global like in Python I can just do global "variable" but the problem is all around me says it is outdated and it is better not to use it and I don't know how to make global variables without it, about screenshots I'll be sure to put code next time, I just joined yesterday so need time to adapt to things here.
Anyways thanks for your help, now I can move on to the next thing I need to code.
 

New Threads

Buy us a coffee!

Back
Top Bottom