2017-04-08 10 views
0

Firebaseデータベースからpidの値を読み取り、このpid値を使用して、データベースからさらに値を取得しています。Firebaseからのデータの取得と表示には時間がかかります

//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(); 
}); 

しかし、このコードを走っ私はこのエラーを取得する:

Uncaught Error: Firebase.child failed: First argument was an invalid path: "". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

PIDエラーがnullであることPID1する予定です。しかし、マークを表示する部分に3秒の遅れを入れると、コードは完全に実行されます。

pidの値が設定される前でも表示マークが実行されます。

テーブル内のpidの値を設定するのに数秒かかります。

問題点は何ですか?なぜデータベースの値をロードするのに時間がかかるのですか?それとも、値を設定することに問題がありますか? firebaseへ

+0

を完了するまで、第2の部分は実行されませんので、別のthen()を追加することができます 'VARのPIDを定義します;' '外.once'前に、右? 'pid1'と同じです。また、 '[i]'とは何ですか?私は宣言された '私は'見ていない、すなわちあなたはforループを使用していない、あなたのためにやっている。最後に、 '.once'メソッドを入れ子にしていますか? 'promise.all'を使うか、それらをネストしたり、' .thens'を連鎖させないでください。 –

答えて

1

要求はそれほど「マークを表示する」コードがコードが完了しました「フェッチ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++; 
    }); 
    }) 
    // 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(); 
    }); 

    }) 
+1

は、関数内にマークコードを表示してその関数を最初に終了して呼び出すこともできます – charlietfl

関連する問題