在Firebase数据库数据检索方面遇到问题

时间:2020-05-04 23:10:43

标签: javascript firebase

我想创建一个登录功能。对于一个项目。它可以正常工作,除了读取数据外,其他所有操作都在检查后进行,因此无论是否可以找到该帐户,都可以登录。我已经尝试了很多事情,其中​​一些已经停止了登录,但是我相信这只是没有及时阅读的问题。但这确实对我制作的其他示例代码有问题。我知道这是一个简单的问题,可能确实很明显,但是如果有人可以借出一些真正有用的知识,那么就花了很多时间。 提交btn媒体来电

function login_4() {
    logIt_3(L, 'login_4', 'submit button pressed');
    if (document.getElementById("inputPName0").value !== "" && document.getElementById("inputPName1").value !== "") {
        //Give this session player names
        pName0 = document.getElementById("inputPName0").value;
        pName1 = document.getElementById("inputPName1").value;
        //DB stuff
        name = newAccount_4('One', "0", pName0);
        logIt_3(L, 'login_4', name);
        if (name == false) {
            return false
        }   
        name = newAccount_4('Two', "1", pName1);
        logIt_3(L, 'login_4', name);
        if (name == false ) {
            return false
        }

        //Populate
        document.getElementById("pName0").innerHTML = pName0;
        document.getElementById("pName1").innerHTML = pName1;
        
        
        //Change screen
        document.getElementById("landingPage").style.display = "none";
        document.getElementById("gamePage").style.display = "block";
        logIt_3(L, 'login_4', 'logged in');
        return
    }
    //error message
    logIt_3(E, "login_4", "cant login without two player names");
    document.getElementById("loginErrMsg").innerHTML = "Please enter a Names for BOTH!";
    return
}

依次调用

function newAccount_4(_player, _playerNum, _pName) {
    name = dbRead_1("users", _pName);
    logIt_3(L, 'newAccount_4', name);
    if (name == false) {
        logIt_3(I, "newAccount_4", "ready to make new account.")
        document.getElementById("loginErrMsg" + _playerNum).innerHTML = "Please enter in new name for player " + _player;
        document.getElementById(("newAcc" + _playerNum)).style.display = "inline";
        return false
    }
    logIt_3(I, "newAccount_4", "Found name: " + _pName);
    return true
}

现在它在控制台中显示了这一点。我将在我的数据库代码下面添加内容,以便可以阅读它。还添加控制台日志,以便您可以查看顺序。

function dbRead_1(_path, _key) {
    logIt_3(L, "dbRead_1", "reading database for: " + _key);
    readStatus = "waiting...";
  
    dbRef = database.ref(_path + '/' + _key).once("value", function(snapshot) {
        if (snapshot.val() != null) {
            var childData = snapshot.val();
            dbRec = {
                name: childData.name,
                score: childData.score
            };
            highScore = childData.score;
            logIt_3(L, "dbRead_1", "read " + dbRec);
            document.getElementById("dbReadStatus").innerHTML = "ok";
            return true;
        
        } 
        else {
            logIt_3(L, "dbRead_1", "couldnt find record");
            document.getElementById("dbReadStatus").innerHTML = "no record";
            return false;
        }
    });
}

我知道控制台记录的日志最好,但是它们具有足够的信息以使至少有意义。我已将它们复制并粘贴到下面

login_4:按下提交按钮

3logging.js:44 dbRead_1:正在读取数据库以供测试通过

3logging.js:44 newAccount_4:未定义

3logging.js:38 newAccount_4:找到名称:testpass

3logging.js:44 login_4:是

3logging.js:44 dbRead_1:读取数据库以获取:testfail

3logging.js:44 newAccount_4:未定义

3logging.js:38 newAccount_4:找到名称:testfail

3logging.js:44 login_4:是

3logging.js:44 login_4:已登录

3logging.js:44 dbRead_1:读取[object Object]

3logging.js:44 dbRead_1:找不到记录

0 个答案:

没有答案