我正在学习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获得考试和科目 . 我调试应用程序但是控件没有进入函数 function(snapshot) 这个脚本是从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并具有正确的值 .