我正在开发一个使用 Fire Base 的网络应用程序 这是我的java脚本代码:
<script src="https://www.gstatic.com/firebasejs/4.3.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyCuG09M2Z10h-sVz3n0tu-lzcNzspiZM5o",
authDomain: "actfirebase.firebaseapp.com",
databaseURL: "https://actfirebase.firebaseio.com",
projectId: "actfirebase",
storageBucket: "actfirebase.appspot.com",
messagingSenderId: "1024677586041"
};
firebase.initializeApp(config);
var rootRef = firebase.database().ref().child("news");
rootRef.on("child_added", snap => {
var name = snap.child("news1").val();
var name2 = snap.child("news2").val();
document.getElementById("fnews").innerHTML = name;
});
</script>
她是 html
<section class="news" id="news" style="opacity:0.01;">
<h3 style="text-align:center;">Latest news</h3>
<article id="newses">
<h2 id="fnews">this is the standers text.!!!</h2>
</article>
<article>
<div id="news21" class="info" style=" text-align:center; width:98%;" >
<h4 Style="color:#ffffff;">Omnis iste natus error </h4>
<p id="news22" style="color:#000066" >Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores (...)</p>
</div>
</article>
<h4 style="color:#ffffff; text-align:center; "><a href="#" style="color:#ffffff;">ARCHIVED NEWS</a></h4>
<!-- / container -->
</section>
我的问题是,当我启动应用程序时,文本发生了变化,这是看台文本。!!!为空
我不知道问题是什么,请帮助我
提前致谢。
最佳答案
您是否监听到了错误的事件?
尝试改变:
rootRef.on("child_added", snap => {
到
rootRef.on("value", snap => {
并且子 news1
将正常加载。
如果您只想在将新子项添加到 Firebase 时加载数据,那么您不必直接访问该引用的子项。这是因为 child_added 只会在任何子项被触发一次,这意味着它没有之前仅添加新子项的子项。
rootRef.on("child_added", snap => {
var name = snap.val(); // <--- omit the .child()
...
关于javascript - fire base 正在检索空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46020236/