如何从 Firebase 数据库动态获取数据

问题描述 投票:0回答:1

我一直在尝试学习在我的项目中使用 firebase 数据库。但是,我无法让它工作,因为从我的 firebase 读取数据时似乎出现错误。我只看了视频教程并从互联网上阅读了一些信息,所以我仍然不确定我在做什么。如果有人能够指出我的代码中的实际错误,我将非常感激。 Here is how it looks like in the database

        const db = getDatabase();
        var tBody = document.querySelector("#tbody1");
        var appointmentID = 0;
        tBody.innerHTML = "";

        function AddRequestToTable(name, email, phone, date, time, service, status) {
            let tRow = document.createElement("tr");
            let td1 = document.createElement("td");
            let td2 = document.createElement("td");
            let td3 = document.createElement("td");
            let td4 = document.createElement("td");
            let td5 = document.createElement("td");
            let td6 = document.createElement("td");
            let td7 = document.createElement("td");

            td1 = ++appointmentID;
            td2 = name;
            td3 = email;
            td4 = phone;
            td5 = date + " " + time;
            td6 = service;
            td7 = status;

            tRow.appendChild(td1);
            tRow.appendChild(td2);
            tRow.appendChild(td3);
            tRow.appendChild(td4);
            tRow.appendChild(td5);
            tRow.appendChild(td6);
            tRow.appendChild(td7);

            tBody.appendChild(tRow);
        }

        function AddAllRequeststoTable(AppointmentRequests) {
            tBody.innerHTML = "";

            AppointmentRequests.array.forEach((element) => {
                AddRequestToTable(
                    element.name,
                    element.email,
                    element.phone,
                    element.date,
                    element.time,
                    element.service,
                    element.status,
                );
            });
        }

        function GetAllDataOnce() {
            const dbRef = ref(db, "appointmentForm");

            get(dbRef, child(db)) = ((snapshot) => {
                var requests = [];

                snapshot.forEach((childSnapshot) => {
                    requests.push(childSnapshot.val());
                });
                AddAllRequeststoTable(requests);
            });
        }

        window.onload = GetAllDataOnce();
    

它说错误出在这部分的某个地方......

function GetAllDataOnce() {
            const dbRef = ref(db, "appointmentForm");

            get(dbRef, child(db)) = ((snapshot) => {
                var requests = [];

                snapshot.forEach((childSnapshot) => {
                    requests.push(childSnapshot.val());
                });
                AddAllRequeststoTable(requests);
            });
        }

我已经尝试了超过12个小时,但仍然无济于事。

javascript html firebase firebase-realtime-database
1个回答
0
投票

您的 GetAllDataOnce 应该是以下内容:

get(child(dbRef, `DBName/${childUID}`)).then((snapshot) => {

} 
© www.soinside.com 2019 - 2024. All rights reserved.