-10

I have been working on a sign up page and my data isn't being stored to the database. Code is below. There are no console errors. I have tried lots of different ideas and I just want data to be stored. Also, another question, is there a way to check if two values in the database are under the same email? I am wondering this for a password system. Can I please get some help. I really am frustrated and want this to work.

<button type="button" id="signUpButton" onclick="confirmData() ; stopClicksAgain()">Sign Up!</button>
<script type="module" src="backend/database.js/"><script>
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.2/firebase-app.js";
import { getDatabase, ref, set  } from "https://www.gstatic.com/firebasejs/9.22.2/firebase-database.js";

const firebaseConfig = {
  apiKey: "AIzaSyDJdI7Sgr7ZGo3Do6NVswgs1UvjVcQUZbs",
  authDomain: "bakerstoolbox-9ca91.firebaseapp.com",
  databaseURL: "https://bakerstoolbox-9ca91-default-rtdb.firebaseio.com",
  projectId: "bakerstoolbox-9ca91",
  storageBucket: "bakerstoolbox-9ca91.appspot.com",
  messagingSenderId: "175444196196",
  appId: "1:175444196196:web:f6314c643c71315b5f1871"
};

const app = initializeApp(firebaseConfig);
const database = getDatabase(app);

//get user sign up data
var email = document.getElementById("email");
var phone = document.getElementById("phone-number");
var firstName = document.getElementById("firstName");
var lastName = document.getElementById("lastName");

//button functions
const signUpButton = document.getElementById("signUpButton");
signUpButton.addEventListener("click", signUpNewUser);

//sign up a new user
function signUpNewUser(){
    set(ref(database, 'users/'), {
        userEmail : email,
        userPhoneNum : phone,
        userFirstName : firstName,
        userLastName : lastName
    })
    .then(() => {
      console.log("GOOD")
    })
    .catch((error) => {
      console.log("ERROR")
    });
}
  • Assuming those are your actual Firebase config values, it's probably best not to expose them to all and sundry ... – Craig Jun 13 '23 at 21:52
  • I think your ref(...) should have a specific ID for the user, like the examples here https://firebase.google.com/docs/database/web/read-and-write – Craig Jun 13 '23 at 21:54
  • I tried that earlier and just now tried it again yet it still doesn't work. – Hehefunnyboi2 Jun 13 '23 at 22:06
  • would i have to define that parameter when calling the function, or just leave it blank? I tried both yet it still didnt work. Just another idea. – Hehefunnyboi2 Jun 13 '23 at 22:09
  • @Craig Firebase configuration values are not secret. You might want to check out: https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public/37484053#37484053 – Frank van Puffelen Jun 14 '23 at 05:18
  • 1
    Hi! Congrats on asking your first question on stack overflow. Some small tips that you might hopefully find useful. Try to avoid composite questions. Like when you state: Also, another question, is there a way to check if two values in the database are under the same email? This is best posed as another question where you provide more context (i.e. a code snippet) more clearly stating what you want to achieve and the code you tried Additionally, it is best to avoid personal statements like "I realy am frustrated and want this to work" given this doesn't add any information to solving the – marwan Jun 14 '23 at 11:22
  • 5
    Possible duplicate of [Why is my data not being stored to my firebase realtime database?](https://stackoverflow.com/questions/76466250/why-is-my-data-not-being-stored-to-my-firebase-realtime-database) – pppery Jun 30 '23 at 01:35
  • 2
    Please don't ask the same question again. If you're not getting an answer to your original question then improve it and you will. However in this case you have an answer there. – Dale K Jun 30 '23 at 03:38

1 Answers1

3

When you run this code:

document.getElementById("email")

You get back an HTML element, not the value that the user put into that field. To get the latter, you'll typically want to read its value property.

See How can I get HTML element value by JavaScript?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807