Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - Firebase throwing error when trying to write data to console

I'm doing a demo for firebase and want to add a user to our database + add a section to our database that holds more user information. The database has no security rules at the moment because its a demo and isn't storing any important information. I get this error whenever I hit the submit button on the webpage:

FIREBASE FATAL ERROR:

Can't determine Firebase Database URL. Be sure to include databaseURL option when calling firebase.intializeApp().

HTML(In a body tag):

<div>
        <h1>SIGNUP ///(88w88)\</h1>
        <input id = "emailS">EMAIL</input>  
        <input id = "passwordS">PASSWORD</input>
        <input id = "name">NAME</input>
        <input id = "phone">PHONE</input>
        <input id = "color">FAVORITE COLOR</input>
        <button id = "submit" onclick = "submitSignUp()">SUBMIT</button>
</div>

<script src="https://www.gstatic.com/firebasejs/7.24.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.8.1/firebase.js"></script>

    <script>
      // Your web app's Firebase configuration
      var firebaseConfig = {
        apiKey: "AIzaSyAOw4BCZhBpKQ1HTaqRXbzj7kFFzviL61Q",
        authDomain: "fir-demo-ca0f5.firebaseapp.com",
        projectId: "fir-demo-ca0f5",
        storageBucket: "fir-demo-ca0f5.appspot.com",
        messagingSenderId: "390968592171",
        appId: "1:390968592171:web:b5856c31026ac491b4e76e"
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
    </script>
    
    <script src = "FirebaseDemoB.js"></script>

JS: - What should happen is that is signsup a user and we set a global variable to true stating that the usersigned up. Once they're logged in, using onStateChanged, we check if they signed up (not logged in) and fetch the information from the forms and write it to the database. In practice I get the error mentioned above

var signedUp = false;

function submitSignUp()
{
    console.log("FIREBASE");
    var email2 = document.getElementById("emailS").value;
    var password = document.getElementById("passwordS").value;
    
    console.log(email2 + " " + password);
    
    firebase.auth().createUserWithEmailAndPassword(email2, password)
      .then((userCredential) => {
        // Signed in 
        user = userCredential.user;
        signedUp = true;
        // ...
      })
      .catch((error) => {
        var errorCode = error.code;
        var errorMessage = error.message;
        // ..
      });
}

function writeUserData(userId, name, phone, color)
{
    firebase.database().ref('users/' + userId).set({
        name: name,
        phone: phone,
        color : color
      });
}

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    if(signedUp)
    {
        var name = document.getElementById("name").value;
        var phone = document.getElementById("phone").value;
        var color = document.getElementById("color").value;
        writeUserData(user.uid, name, phone, color);
        
    }
    // ...
  } else {
    console.log("HELLOTHERE");
  }
});
question from:https://stackoverflow.com/questions/66049819/firebase-throwing-error-when-trying-to-write-data-to-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The configuration snippet in your code doesn't contain a databaseURL property, which is needed if you want to use the Firebase Realtime Database.

Most likely this is because you copied the configuration snippet before the database was created. You can just copy the updated configuration snippet from the Firebase console, and add it to your code, to fix the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...