How can I fire onclick click on this function with firebase?

Asked

Viewed 36 times

-1

I need to fire the DB check for an input. Can anyone help me? I saw some similar doubts here but could not resolve.

var firebaseConfig = {
    apiKey: "AIzaSyBd-oV4iz3VnEL41DnNQrXcaQAKT4hPRU0",
    authDomain: "suvs-e02e4.firebaseapp.com",
    databaseURL: "https://suvs-e02e4.firebaseio.com",
    projectId: "suvs-e02e4",
    storageBucket: "suvs-e02e4.appspot.com",
    messagingSenderId: "156256669497",
    appId: "1:156256669497:web:a618f3f479291792e1a2e8"
};
  
document.getElementById('disp').onclick = () => {
  firebase.initializeApp(firebaseConfig);

  let SSO = document.getElementById('inputSso').value
  let db = firebase.firestore();
  let docRef = db.collection("SSOs").doc("NvZDEAmhG2Usyu2UW4Om")

  docRef.get().then((doc) => {
    let SSOs = doc.data().Ssos;
    let Arsso = SSOs;
    Arsso.includes(SSO) ? alert ("TEM") : alert("não tem")
  })
}
<div class="form-group mx-sm-3 mb-2">
  <label for="inputPassword2" class="sr-only"></label>
  <input type="text" class="form-control" id="inputSso" placeholder="SSO">
</div>

<button type="submit" class="btn btn-primary mb-2" id="disp">Checar</button>

1 answer

0


Apparently it is only necessary to put the initializeApp firebase OUT of onclick. Since you didn’t include error messages or more information, I assume the rest is correct and firebase has been added to your page. Follow code with changes (and working).

var firebaseConfig = {
    apiKey: "AIzaSyBd-oV4iz3VnEL41DnNQrXcaQAKT4hPRU0",
    authDomain: "suvs-e02e4.firebaseapp.com",
    databaseURL: "https://suvs-e02e4.firebaseio.com",
    projectId: "suvs-e02e4",
    storageBucket: "suvs-e02e4.appspot.com",
    messagingSenderId: "156256669497",
    appId: "1:156256669497:web:a618f3f479291792e1a2e8"
};

firebase.initializeApp(firebaseConfig);

document.getElementById('disp').onclick = () => {
  let SSO = document.getElementById('inputSso').value
  
  let db = firebase.firestore();
  let docRef = db.collection("SSOs").doc("NvZDEAmhG2Usyu2UW4Om");
  docRef.get()
    .then((doc) =>
    {
      let SSOs = doc.data().Ssos;
      document.getElementById('resultado').innerHTML = SSOs;
      SSOs.includes(SSO) ? alert("TEM") : alert("não tem");
    });
}
<script type="text/javascript" src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/firebasejs/7.14.2/firebase-firestore.js"></script>

<div class="form-group mx-sm-3 mb-2">
  <label for="inputPassword2" class="sr-only"></label>
  <input type="text" class="form-control" id="inputSso" placeholder="SSO">
</div>
 
<button type="submit" class="btn btn-primary mb-2" id="disp">Checar</button>

<div id="resultado"></div>

PS: note the line let Arsso =SSOs; was removed as it was unnecessary.

  • Thank you very much! That’s exactly it, thanks also for the edition!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.