Firebase数据检索无效

时间:2017-02-13 00:54:41

标签: javascript firebase firebase-realtime-database

我一直在尝试检索Firebase数据库中的所有条目以填充我的HTML页面,但我无法弄明白。我已经关注了许多教程和Firebase文档,但我的代码在某种程度上没有更改HTML页面,并且控制台中没有显示错误。

基本上,我所拥有的是数据库中的作业条目列表,我需要以下图的格式发布这些作业条目。

enter image description here

例如,我在FirebaseDB中有这么多作业

enter image description here

我想检索所有这些条目并显示四张卡片及其各自的信息,并且在添加新条目时,它们将被添加到显示器中。现在我的代码不起作用,我无法弄清楚原因。任何帮助将不胜感激。

  <div class="row" id='Card'>
<article class="col-xs-4">
  <div class="cards"><span class="glyphicon glyphicon-flash icon"></span>
    <hr class="divider"/>
    <h2 class="title" id="jobTitle">Job Title</h2>
    <div class="info">
      <hr class="divider"/>
      <p class="lead" id="jobDescription"><Strong>This is where Job description goes</strong></p><a class="btn btn-lg center-block" onclick="location.href = 'Specialistscard.html';">Specialists</a>
    </div>
  </div>
</article>



var jobTitle = document.getElementById('jobTitle');
var jobDescription= document.getElementById('jobDescription');

database = firebase.database();

var ref = database.ref('Jobs');
ref.on('value', gotData, errData);


function gotData(data) {
  console.log(data);

  var jobs = data.val();
  var keys = Object.keys(jobs);

  for (var i = 0; i < keys.length; i++) {
    var k = keys[i];
    jobTitle = jobs[k].JobTitle;
    jobDescription = jobs[k].JobDescription;

    var li = document.createElement('card', jobTitle, jobDescription)

  }
}

 function errData(err) {
  console.log('Error!');
  console.log(err);
}

1 个答案:

答案 0 :(得分:1)

Ok @Ola这是一个如何通过javascript填充你的html的例子,你也可以看看HandleBars是一个JS库来操纵DOM并拥有其他许多功能。

问候!

&#13;
&#13;
var dummyDataJobs = [
    {
        title: 'CEO',
        description: 'CEO Description'
    },
    {
        title: 'CTO',
        description: 'CTO Description'
    },
    {
        title: 'VP',
        description: 'VP Description'
    }
]
var container = document.getElementById('jobsContainer');
for (var index = 0; index < dummyDataJobs.length; index++) {
    var job = dummyDataJobs[index];
    var newCard = `
<div class="row" id='Card'>
<article class="col-xs-4">
  <div class="cards"><span class="glyphicon glyphicon-flash icon"></span>
    <hr class="divider"/>
    <h2 class="title">`+ job.title + `</h2>
    <div class="info">
      <hr class="divider"/>
      <p class="lead"><Strong>`+ job.description + `</strong></p><a class="btn btn-lg center-block" onclick="location.href = 'Specialistscard.html';">Specialists</a>
    </div>
  </div>
</article>`;

    container.innerHTML += newCard;
}
&#13;
<!DOCTYPE html>
<html>
<body>
<div id="jobsContainer"></div>
</body>
</html>
&#13;
&#13;
&#13;

在您的情况下,您可以尝试以下循环:

var container = document.getElementById('jobsContainer');
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
var newCard = `
        <div class="row" id='Card'>
        <article class="col-xs-4">
        <div class="cards"><span class="glyphicon glyphicon-flash icon">   </span>
            <hr class="divider"/>
            <h2 class="title">`+ jobs[k].JobTitle + `</h2>
            <div class="info">
            <hr class="divider"/>
            <p class="lead"><Strong>`+ jobs[k].JobDescription + 
            `</strong></p><a class="btn btn-lg center-block" onclick="location.href = 'Specialistscard.html';">Specialists</a>
            </div>
        </div>
        </article>`;

container.innerHTML += newCard;}//END OF FOR

请务必在html中添加此<div id="jobsContainer"></div>,例如