繁体   English   中英

从Firebase提取和显示数据需要时间

[英]Fetching and displaying data from Firebase takes time

我正在从Firebase数据库读取pid的值,然后使用此pid值从数据库中获取更多值。

//fetching pid
firebaseRef.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      //getting key of the child
      var pid = childSnapshot.key;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();
      pid[i].id = "pid" + (i + 1);
      document.getElementById(pids[i].id).innerText = pid;
      i++;
    });
  });

//displaying marks
var pid1 = document.getElementById("pid1").innerHTML;
guideRef = firebase.database().ref("internal").child(pid1).child("guide");
guideRef.child("report").once('value').then(function(snapshot) {
  document.getElementById(reportGuideMarks).value = snapshot.val();
});

但是运行这段代码我得到这个错误:

未捕获的错误:Firebase.child失败:第一个参数是无效路径:“”。 路径必须是非空字符串,并且不能包含“。”,“#”,“ $”,“ [”或“]”

pid错误是由于pid1为null。 但是,当我在“显示标记”部分放置3秒的延迟时,代码可以完美运行。

显示标记甚至在设置pid的值之前执行。

在表中设置pid的值需要花费几秒钟。

有什么问题 为什么加载数据库的值需要这么多时间? 还是设置值有问题?

对Firebase的请求是异步的,因此“显示标记”代码将在“获取pid”代码完成之前运行。

您可以添加另一个then()以便第二部分在第一部分完成之前不会运行

//fetching pid
firebaseRef.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      //getting key of the child
      var pid = childSnapshot.key;
      // childData will be the actual contents of the child
      var childData = childSnapshot.val();
      pid[i].id = "pid" + (i + 1);
      document.getElementById(pids[i].id).innerText = pid;
      i++;
    });
  })
  // new then() won't fire until prior then() is completed
  .then(function() {    
    //displaying marks
    var pid1 = document.getElementById("pid1").innerHTML;
    guideRef = firebase.database().ref("internal").child(pid1).child("guide");
    guideRef.child("report").once('value').then(function(snapshot) {
      document.getElementById(reportGuideMarks).value = snapshot.val();
    });

  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM