Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
local storage being overwriten instead of adding to array

I am building a simple login web page with JavaScript and using Chrome. I have an area for login and one for registering a new user. My problem is, I have created an array and am storing it on the local storage. Every time a new registry happens its values override the previous ones on the Local storage. How can I make it so that the new users information is added to the array while keeping the previous ones? Code follows: `

const loginForm = document.getElementById("loginForm");
const loginButton = document.getElementById("loginButton");
const loginError = document.getElementById("loginError");

const registerForm = document.getElementById("registerForm");
const registerButton = document.getElementById( "registerButton");
const registerError = document.getElementById("registerError");
  
loginButton.addEventListener("click", (e)=> {
    e.preventDefault();

    const username = loginForm.username.value;
    const password = loginForm.password.value;
    /*WILL WORK ON THIS AFTER REGISTER IS OKAY*/
    if(username === "user" && password ==="123"){
        alert("Login sucessful!");
        location.reload();
    } else {
        loginError.style.opacity = 2;

    }
})

registerButton.addEventListener("click", (e)=> {
    e.preventDefault();

    const registerUsername = registerForm.registerUsername.value;
    const registerPassword = registerForm.registerPassword.value;
    const confirmPassword = registerForm.confirmPassword.value;
    const registerEmail = registerForm.registerEmail.value;

    const userData = {registerUsername, registerPassword};
    let registredUserData = [];
   

    if(registerPassword.length > 8 && registerPassword.match(/[a-z]+/) && registerPassword.match(/[A-Z]+/) && registerPassword.match(/[0-9]+/) &&  registerPassword.match(/[$@#&!]+/) && registerPassword === confirmPassword && registerEmail.match(/[@/]+/ ) ){      

        /*ERROR HERE.LOCAL STORAGE IS OVERWRITTEN*/ 
        JSON.parse(localStorage.getItem("registred users"));
        registredUserData.push(userData);
        localStorage.setItem("registred users",JSON.stringify(registredUserData));
            

        location.reload();
    } else {
        registerError.style.opacity = 2;

    }
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not assigning a variable registredUserData in line after a comment. That section should probably look like this.

registredUserData = JSON.parse(localStorage.getItem("registred users"));
registredUserData.push(userData);
localStorage.setItem("registred users",JSON.stringify(registredUserData));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda