【问题标题】:Can't get Firebase output without including Parent key如果不包含父键,则无法获得 Firebase 输出
【发布时间】:2017-03-19 17:30:11
【问题描述】:

我的 uni 小组正在尝试创建一个简单的博客应用程序,但在不使用 JSON.stringify 的情况下将 firebase 数据输出到网站时遇到了问题。目前我们拥有的代码是

dbRefObject.on('value',snap =>
    {
        preObject.innerText = JSON.stringify(snap.val(), null, 3);
    });
    dbRefList.on("value", function(snapshot)
    {
        var newPost = snapshot.val();

    dbRefList.on("child_added", function(newPost,pre)
        {
        newchild=newPost.val();
        ulList.innerHTML = "<pre>" + "Title: "+ newchild.Title +
        "\n"+"Message: "+ newchild.Message +
        "\n"+ "Posted: "+ newchild.Posted +"</pre>";
        });

preObject.innerText 的输出是。

   {
   "Posts": {
      "-KfXaHa8MKi4Qzsro3Hk": {
         "Message": "out with friends",
         "Posted": "Date: 18/3/2017 @ 18:6:13",
         "Title": "time out"
      },
      "-KfZ3r08lhIuEM9mb5GV": {
         "Message": "this is a test",
         "Posted": "Date: 19/3/2017 @ 0:59:24",
         "Title": "testing"
      },
      "-KfZY8zgUDJU_SooEvEO": {
         "Message": "03:11 am and this is kind of working.",
         "Posted": "Date: 19/3/2017 @ 3:11:46",
         "Title": "the time is now"
      },
      "-KfZiAijlWNU6-1RGdh9": {
         "Message": "The time is now 03:59 and things are coming good finally.",
         "Posted": "Date: 19/3/2017 @ 3:59:57",
         "Title": "Latest Progress"
      },
      "-KfZrtZU4pkgejOQ8N78": {
         "Message": "Ok time is 04:42 going to my bed.",
         "Posted": "Date: 19/3/2017 @ 4:42:24",
         "Title": "Final Post"
      },
      "-Kf_H-Lg2KOIjtOdALEf": {
         "Message": "5676576756575",
         "Posted": "Date: 19/3/2017 @ 6:36:27",
         "Title": "5675675"
      }
   }
}

我们希望代码在不显示键的情况下输出每个键的子项。我正在努力在 firebase 文档上找到任何可以帮助解决此问题的内容,所以我想问一下,希望我已经提供了足够的信息,谢谢。

【问题讨论】:

    标签: javascript json firebase firebase-realtime-database


    【解决方案1】:

    您可以遍历每个帖子做事:

    var obj = snap.val();
    for (var key in obj.Posts) {
      var post = obj[key];
      console.log(post.Title + ", " + post.Message + ", " + post.Posted);
    }
    

    (变量key 包含您需要的字母数字键)

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      相关资源
      最近更新 更多