我一直在尝试从Firebase输出多个值到我的HTML文档,但是,我可以使用console.log获取所有子输出,但是当我尝试将其添加到HTML时,它只输出1个值。我试图使用forEach,但我不能让它工作(不确定我是否犯了错误)。
到目前为止我的代码是
const dbRefObjectPosts = dbRefObject.child('Posts');
dbRefObjectPosts.on("value", function(snap){
snap.forEach(function(childSnapshot){
document.getElementById('object').innerHTML = (
"Title: " + childSnapshot.val().Title + "</br>" +
"Message: " + childSnapshot.val().Message + "</br>" +
"Posted: " + childSnapshot.val().Posted
);
console.log(childSnapshot.val().Message);
});
});
html输出
Title: 6756775
Message: 56756575776
Posted: Date: 19/3/2017 @ 23:2:40
console.log输出是
out with friends
BLOG.js:61 this is a test
BLOG.js:61 03:11 am and this is kind of working.
BLOG.js:61 The time is now 03:59 and things are coming good finally.
BLOG.js:61 Ok time is 04:42 going to my bed.
BLOG.js:61 5676576756575
BLOG.js:61 56756575776
答案 0 :(得分:2)
此脚本将解决您的问题。
dbRefObjectPosts.on("value", function(snap){
snap.forEach(function(childSnapshot){
document.getElementById('object').innerHTML += (
"Title: " + childSnapshot.val().Title + "</br>" +
"Message: " + childSnapshot.val().Message + "</br>" +
"Posted: " + childSnapshot.val().Posted +
"<br/><br/>"
);
console.log(childSnapshot.val().Message);
});
});