简体   繁体   English

将数据从 firebase 显示到 HTML 表格

[英]Display data from firebase to HTML table

I would like to ask some help, I'm a front-end developer and we currently have a project/task that is all about the back end.我想寻求一些帮助,我是一名前端开发人员,我们目前有一个关于后端的项目/任务。 My team members decided to used firebase as a database in our web application.我的团队成员决定在我们的 Web 应用程序中使用 firebase 作为数据库。 I can now send data to the database but I have a problem, I cant retrieve the data from firebase to the web application.我现在可以将数据发送到数据库,但是我遇到了一个问题,我无法将数据从 firebase 检索到 Web 应用程序。 I am planning to retrieve data and display it in my table (HTML).我计划检索数据并将其显示在我的表格 (HTML) 中。

This is my code in my javascript for retrieving data from firebase:这是我在 javascript 中用于从 firebase 检索数据的代码:

var database = firebase.database().ref().child('Tasks');
database.once('value', function(snapshot){
    if(snapshot.exists()){
        var content = '';
        var TaskTitle = snapshot.val().TaskTitle;
        var JobId= snapshot.val().JobId;

        snapshot.forEach(function(data){
        });

        content = '<tr>';
        content += '<td>' + TaskTitle + '</td>'; //column1
        content += '<td>' + JobId + '</td>';//column2
        content += '</tr>';
    }

    $('#ex-table').append(content);
    console.log(snapshot.val());
});

And this is my code in my HTML table to display the data from the database:这是我在 HTML 表中的代码,用于显示数据库中的数据:

<table class="table table-striped" id="ex-table">
    <thead class="thead-inverse">
        <tr>
        <th>Task Title</th>
        <th>Category</th>
        <th>Date</th>
        <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <tr id="tr">
        <td id="titleTask"></td>
        <td id="categoryTask"></td>
        <td id="dateTask"></td>
        <td id="statusTask"></td>
       </tr>
   </tbody>

I cant actually see the data that I have retrieved using the console in my web browser in chrome.我实际上无法在 Chrome 浏览器中查看使用控制台检索到的数据。

and it's displaying this: Data displayed in console while in my web app it's displaying like this: undefined它显示的是:数据显示在控制台中,而在我的网络应用程序中,它显示如下:未定义

Can someone please help me, I'm new to everything your help is so much appreciated.有人可以帮助我吗,我对一切都很陌生,非常感谢您的帮助。

Your nesting is a bit wonky, and unfortunately it matters quite a lot where you put all the bits: 你的嵌套有点不稳定,不幸的是,你把所有的东西放在一起很重要:

var database = firebase.database().ref().child('Tasks');
database.once('value', function(snapshot){
    if(snapshot.exists()){
        var content = '';

        snapshot.forEach(function(data){
            var TaskTitle = data.val().TaskTitle;
            var JobId= data.val().JobId;
            content += '<tr>';
            content += '<td>' + TaskTitle + '</td>'; //column1
            content += '<td>' + JobId + '</td>';//column2
            content += '</tr>';
        });

        $('#ex-table').append(content);
    }
});

In steps: 步骤:

  1. This loads the tasks from Firebase, with database.once() . 这将使用database.once()从Firebase加载任务。
  2. Then if there are any tasks (one level indented), it loops over them (with snapshot.forEach(...) ). 然后,如果有任何任务(一个级别缩进),它将循环遍历它们(使用snapshot.forEach(...) )。
  3. In that callback (so one more level indented) it takes the title and job id for that specific task, and adds it to the HTML string it is building. 在该回调中(缩进一个级别),它获取该特定任务的标题和作业ID,并将其添加到它正在构建的HTML字符串中。
  4. The finally it adds the HTML to the table. 最后它将HTML添加到表中。
    <table border="1" style="width:100%" id="ex-table">
        <tr id="tr">
            <th>Student ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Address</th>
            <th>Username</th>
            <th>Password</th>
    </table>

const starCountRef = ref(database, 'user_register/'); const starCountRef = ref(database, 'user_register/'); onValue(starCountRef, (snapshot) => { onValue(starCountRef, (快照) => {

   snapshot.forEach(function (data) {
       var val = data.val();

       var content = '';


       content += '<tr>';
       content += '<td>' + val.student_id + '</td>'; //column1
       content += '<td>' + val.first_name + '</td>';//column2
       content += '<td>' + val.last_name + '</td>';//column2
       content += '<td>' + val.email + '</td>'; //column1
       content += '<td>' + val.address + '</td>';//column2
       content += '<td>' + val.username + '</td>';//column2
       content += '<td>' + val.password + '</td>';//column2
       content += '</tr>';

       $('#ex-table').append(content);


   });

   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM