javascript - fire base 正在检索空值

标签 javascript html firebase web firebase-realtime-database

我正在开发一个使用 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/

相关文章:

javascript - 简单的 API 调用 (CORS) 不起作用

c# - Xenu Link Sleuth 通过代理服务器

html - 如何连续呈现 django 博客文章?

javascript - 如何在 React 中的切换开关组件内添加文本?

javascript - 如何将 key 添加到添加到 Angularfire 2 版本 5 中节点的对象

ios - firebase auth 用户照片 URL 返回 404

javascript - 如何在输入测试下方显示列表?

javascript - 包含 4 位数字和 2 个可选字母的邮政编码的正则表达式

javascript - 如何禁用 HTML/JS 中的 STYLE 元素?

node.js - 使用 Node.js 函数从 Firebase 获取值键对