Firebase检索数据不适用于我的网站

时间:2016-07-08 17:41:34

标签: javascript firebase firebase-realtime-database

我正在学习Firebase。我正在尝试从Firebase读取数据,但不会触发功能。以下是我的javascript代码。 的 getCount将()

var exam = localStorage.EXAM;
var subject = localStorage.SUBJECT;
var ref = new Firebase(FIREBASE_URL);
var TotQuestion = ref.child(exam).child(subject).child("TotQuestion");
TotQuestion.on("value", function(snapshot) 
{
    if(snapshot.child("count").val() != null) 
    {
      var count = snapshot.child("count").val();
        if(count == null)
          {
            localStorage.setItem("COUNT", "0");
            window.location.href="QAdd.html";
          }else{
            localStorage.setItem("COUNT", count);
            window.location.href="QAdd.html";
          }
    }   


},function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

我从上一个屏幕的localStorage获得考试和科目。我调试应用程序但是控件没有进入功能功能(快照) 从html表单单击按钮时触发此脚本。请帮忙。 谢谢 迪帕克

更新问题:

我有两个功能。 的 myFunction的

if (typeof(Storage) !== "undefined") 
            {
                localStorage.setItem("SUBJECT", subject);
                getCount();


            } else 
            {
                    document.getElementById("error").innerHTML = "Sorry, your browser does not support Web Storage...";
            }

在上面的代码中我调用了getCount()函数。我在哪里访问Firebase并获得计数。程序在将null值设置为innerHTML时失败。 document.getElementById(" error")。innerHTML = localStorage.COUNT; 原因是系统没有进入Firebase调用并将innerHTML设置为null。

现在我遇到了错误。

FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot set property 'innerHTML' of null
at file:///D:/Firebase%20Web/Sub.html:84:55
at c (https://cdn.firebase.com/js/client/2.4.2/firebase.js:242:58)
at https://cdn.firebase.com/js/client/2.4.2/firebase.js:203:710
at gc (https://cdn.firebase.com/js/client/2.4.2/firebase.js:52:165)
at cc (https://cdn.firebase.com/js/client/2.4.2/firebase.js:30:216)
at dc (https://cdn.firebase.com/js/client/2.4.2/firebase.js:29:1259)
at Ji.h.Kb (https://cdn.firebase.com/js/client/2.4.2/firebase.js:222:287)
at Rh.h.Ld (https://cdn.firebase.com/js/client/2.4.2/firebase.js:189:251)
at Fh.Ld (https://cdn.firebase.com/js/client/2.4.2/firebase.js:179:364)
at kh.nc (https://cdn.firebase.com/js/client/2.4.2/firebase.js:177:280) 

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

现在已经解决了。下面是正确的代码。

myFunction的

if (typeof(Storage) !== "undefined") 
                {
                    localStorage.setItem("SUBJECT", subject);
                    getCount();


                } else 
                {
                        document.getElementById("error").innerHTML = "Sorry, your browser does not support Web Storage...";
                }


function getCount(){
                var exam = localStorage.EXAM;
                var subject = localStorage.SUBJECT;
                var ref = new Firebase(FIREBASE_URL);
                var TotQuestion = ref.child(exam).child(subject).child("TotQuestion").child("count");
                TotQuestion.once("value",function(snapshot){
                        if(snapshot.val() != null) 
                        {
                                var count = snapshot.val();
                                if(count == null)
                                    {
                                        localStorage.setItem("COUNT", "0");
                                        window.location.href="QAdd.html";
                                    }else{
                                        localStorage.setItem("COUNT", count);
                                        window.location.href="QAdd.html";
                                    }

                        }   


                },function (errorObject){
                        console.log("The read failed: " + errorObject.code);
                            });
        }

这已成功将控件转移到QAdd.html并且值正确。

0 个答案:

没有答案