Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
I couldn't push the object person to an array that is stored in local storage, the erorr is (Cannot read properties of null (reading 'push'))
<p class="center" >
        name:<br> <input type="text" id="nameinput" required><br>
        email:<br><input type="email" id="emailinput" required><br>
        password:<br><input type="password" id="passwordinput" required><br>
        <button id="registerbutton" onclick="co`enter code here`ntainsObject()">Register<button>
    <p><br><p id="feedback"></p><br>
    <button onclick="clearStorage()">Clear Storage</button>
   
     <body id="body">
        <script>
            var userArray = [];
            var person = {};
            var newemail;
            var oldemail;
            var feedback = document.getElementById("feedback");
            person.name = document.getElementById("nameinput").value;
            person.email = document.getElementById("emailinput").value;
            person.password = document.getElementById("passwordinput").value;
            JSON.parse(localStorage.getItem(userArray));
            
            function containsObject(newemail, userArray) {
                newemail = document.getElementById("emailinput").value;
            
                oldemail = localStorage.email;
            
                if (newemail === oldemail) {
                    feedback.innerHTML = "email exist";
                } else {
                    storeName();
                }
            }
            
            
            function storeName(person, userArray) {
            
            
                let newarray = JSON.parse(localStorage.getItem(userArray));
                newarray.push(person);
                userArray = newarray;
                localStorage.setItem("person", JSON.stringify(userArray));
                console.log(userArray);
            }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hi Mohammed when using the local storage you always must first initialize a value before altering it. Here an example : Initializing :

  localStorage.setItem('user1', 'Mohammed');
Then you can query the local storage to retrieve the value :

localStorage.getItem('user1')

Or you can overwrite its value :

localStorage.setItem('user1','Jip')

Important to know is that both the key and its assigned value must be strings. So when you want to store and initialize an array of users the array must be stored as a string. To repeat the process for your use case : Initializing :

localStorage.setItem('users',"[]")

Reading :

localStorage.getItem('users')

Altering its value :

let users = JSON.parse(localStorage.getItem('users'))
users.push('Jip')
users = JSON.stringify(users)
localStorage.setItem('users',users)

I hope this is what you needed.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda