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

223
Visualizações
Why Am I Not able to Write Data To Firebase using Js via Html?

I've just tried To write a very basic data to my firebase database but it isn't working. Here's is the code that i've used

  <script src="https://www.gstatic.com/firebasejs/9.8.4/firebase-database.js"></script>
<script type="module">
  // Import the functions you need from the SDKs you need
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.4/firebase-app.js";
  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: ""
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
function writeUserData() {
          firebase.database().ref('users').set({ uId: document.getElementById('fname').value,
            refercode:document.getElementById('lname').value})
            </script>
    <form name="myForm">
      <div class="col-25">
        <label for="fname">text1</label>
      </div>
      <div class="col-75">
        <input type="text" id="fname" name="firstname" placeholder="Your PlayStore Email.." required></input>
         <div class="col-25">
           <label for="lname">text2</label>
         </div>
         <div class="col-75">
           <input type="text" id="lname" name="codename" placeholder="Your PlayStore Email.." required></input>
      </div> <button onclick="writeUserData()" class="button">Request Withdrawal</button></form>

But this isn't writing any data on to my database. It returns an error in console, "writeUserData" is not defined.How can i solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since your script block is typed as a module, the internals of it are not available globally.

One way to make your writeUserData available to the button click handler is to add it to the window object as shown here: JS modules - ReferenceError: <function> is not defined

But a better solution is to not depend on an inline onclick handler, and instead attach the handler in the script module itself with addEventListener like:

<script type="module">
  ...
  function writeUserData() {
    firebase.database().ref('users').set({ uId: 
      document.getElementById('fname').value,
      refercode:document.getElementById('lname').value
    })
  }

  // 👇
  document.querySelector('button').addEventListener('click', writeUserData)
</script>
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