我的函数javascript不';我不能完全工作
我的javascript函数不能完全工作 HTML:我的函数javascript不';我不能完全工作,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我的javascript函数不能完全工作 HTML: <body onload="initialize();"> <div class="topnav"> <a href="./index.html">Accueil</a> <a class="active" href="./mycave.html">Ma cave</a> <a href="./mylist.html"&
<body onload="initialize();">
<div class="topnav">
<a href="./index.html">Accueil</a>
<a class="active" href="./mycave.html">Ma cave</a>
<a href="./mylist.html">Ma liste</a>
<a href="./account.html">Mon compte</a>
</div>
<div style="padding-left:16px" id="main">
</div>
加载文档时,日志控制台仅写入“函数终止”
当我在控制台中执行该函数时,控制台正在写入'Vin:cn{node_u3;Je,ref_3;Oi,index_3;Pe}',然后写入'function terminated'
有人能告诉我它可能来自哪里吗?这是因为
dbref.once('value')。然后,…
部分在获得值后异步执行。这意味着程序将跳过dbref.once
部分并继续执行,将其降落在console.info
部分。(对计算机)一段时间后,dbref.once('value')。然后执行…
部分,可能在函数完成后很久
基本上,您的
dbref.once('value')。然后…
函数将仅在数据存在并准备就绪时执行,这需要时间,因此它是异步完成的,计算机将在等待数据时继续执行。您必须计划稍后在未知时间执行的函数,因此输入占位符值可能是一个不错的选择,函数可以用实际数据覆盖这些默认值。这是因为函数(快照){
在数据库中发生值
事件时执行,而不是立即执行
function initialize(){
var main = document.getElementById('main');
var database = firebase.database();
var dbref = database.ref(uid+'/cave');
dbref.once('value').then(function(snapshot){
snapshot.forEach(function(childsnapshot){
console.info('Vin : ', childsnapshot);
var div = document.createElement('div');
div.setAttribute('class', 'card');
var header = document.createElement('h1')
header.appendChild(document.createTextNode(childsnapshot.val().nom));
div.appendChild(header);
var viticulteur = document.createElement('p');
viticulteur.appendChild(document.createTextNode(childsnapshot.val().viticulteur));
div.appendChild(viticulteur);
var nb_bouteilles = document.createElement('p');
nb_bouteilles.setAttribute('class', 'price');
nb_bouteilles.appendChild(document.createTextNode(childsnapshot.val().number));
div.appendChild(nb_bouteilles);
var open = document.createElement('p');
var open_btn = document.createElement('button');
open_btn.appendChild(document.createTextNode('Voir'));
open.appendChild(open_btn);
div.appendChild(open);
main.appendChild(div);
});
});
console.info('Function terminated!');
}