Sunday, 2 October 2022

Firebase Web Read from Real-Time Database

I'm trying to display the user's price that they entered in the database, but I'm getting "undefined' back instead of the value that was entered. I didn't get any errors in the console either. How can I fix this? I am using html, js, and css. I have provided a screenshot and my code. Thanks!

Studio Dashboard JS:

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Initialize variables
const database = firebase.database();
const auth = firebase.auth();

//const auth = getAuth();
firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        readData();
        // ...
    } else {
        window.location.href = "login.html?error";
        alert("No active user please sign or sign up.");
    }
});

function readData() {

    const user = firebase.auth().currentUser;

    database.ref('/studiopick/studio/users/' + user.uid).get().then(snapshot => {

        //Tab One Display
        document.getElementById("studioName").innerText = snapshot.val().studioName;
        document.getElementById("profile-name").innerText = snapshot.val().studioName;
        document.getElementById("firstName").innerText = snapshot.val().firstName;
        document.getElementById("lastName").innerText = snapshot.val().lastName;
        document.getElementById("lastName").innerText = snapshot.val().lastName;
        document.getElementById("email").innerText = snapshot.val().email;
        document.getElementById("phoneNumber").innerText = snapshot.val().phoneNumber;

        //Tab Two Display
        document.getElementById("servicePrice").innerText = snapshot.val().numberInput;
    }).catch(e => { console.log(e) })
}

function updatePrice() {
    //Get data
    numberInput = document.getElementById("numberInput").value;

    const user = firebase.auth().currentUser;

    //Enter database location
    firebase
        .database()
        .ref('/studiopick/studio/users/' + user.uid + "/prices/roomA/serviceOne")
        .update({
            //studioName : studioName,
            numberInput: numberInput,
        });
}

enter image description here

enter image description here



from Firebase Web Read from Real-Time Database

No comments:

Post a Comment