gpt4 book ai didi

javascript - 如果不包含父 key ,则无法获取 Firebase 输出

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:43 26 4
gpt4 key购买 nike

我的大学小组正在尝试创建一个简单的博客应用程序,但我们在不使用 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"
}
}
}

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

最佳答案

您可以遍历每个帖子的操作:

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

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

关于javascript - 如果不包含父 key ,则无法获取 Firebase 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com