数据无法加载firebase.google.com数据库

时间:2016-05-24 22:42:35

标签: javascript firebase firebase-realtime-database firebase-authentication

我试图保存并将我的播放器的用户名加载到firebase数据库,但由于某种原因它无法正常工作。当我使用警报来调试它时,用户名为null。我在html文件的标签中初始化firebase。这是我的用户名保存和加载代码。保存工作,但不加载:

var signedIn = false;
var player;
var isAnonymous;
var userID;

var username = null;

window.onload = function() {
    auth.onAuthStateChanged(function(user) {
        //var user = firebase.auth().currentUser;
        if (user) {
            // User is signed in.
            player = firebase.auth().currentUser;
            signedIn = true;
            isAnonymous = user.isAnonymous;
            userID = user.uid;
            refreshUsername();
        } else {
            // No user is signed in.
            signedIn = false;
            window.location.href = "index.html";
        }
    });
}

function refreshUsername() {
    if (username != null && username != "") {
        updateUsername();
        document.getElementById("username-text").innerHTML += username;
    } else {
        updateUsername();
        alert("old Not working: " + username);
        var newName = getRandomName();
        document.getElementById("username-text").innerHTML += newName;
        saveUsername(newName);
        username = newName;
        alert("Not working: " + username);
    }
}

function saveUsername(name) {
    firebase.database().ref('users/' + userID).set({
        username: name
    });
}

function updateUsername() {
    /*firebase.database().ref('/users/' + userID).once('value').then(function(snapshot) {
        username = snapshot.val().username;
    });*/
    firebase.database().ref('users/' + userID).once('value').then(function(snapshot) {
        username = snapshot.val().username;
    });
}

1 个答案:

答案 0 :(得分:1)

我设法让它发挥作用。 firebase的内容花费了很长时间,其余的代码在用户名更新之前完成了。我通过使用回调来修复它。这是我的工作代码:

var signedIn = false;
var player;
var isAnonymous;
var userID;

var username = null;

window.onload = function() {
    auth.onAuthStateChanged(function(user) {
        //var user = firebase.auth().currentUser;
        if (user) {
            // User is signed in.
            player = firebase.auth().currentUser;
            signedIn = true;
            isAnonymous = user.isAnonymous;
            userID = user.uid;
            refreshUsername();
        } else {
            // No user is signed in.
            signedIn = false;
            window.location.href = "index.html";
        }
    });
}

function refreshUsername() {
    document.getElementById("username-text").innerHTML = "Username: " + "Loading...";
    updateUsername(function() {
        if (username != null) {
            document.getElementById("username-text").innerHTML = "Username: " + username;
        } else {
            randomUsername();
        }
    });   
}

function saveUsername(name) {
    firebase.database().ref('users/' + userID).set({
        username: name
    });                         
}

function updateUsername(_callback) {
    firebase.database().ref('users/' + userID).once('value').then(function(snapshot) {
        username = snapshot.val().username;
        _callback();
    });
}