1

你好

这是我的第一个问题。我希望我不会做任何愚蠢的事情。

我正在尝试从 html 上的 firestore 获取数据。我看了一些项目,但没有成功。我的代码如下。我收到了这个错误:Uncaught (in promise) ReferenceError: collection is not defined

我的 Firebase 版本已更新。实际上,我想解析firebase数据而不是表它,并额外解析我从web url列表中获得的json。谢谢你的帮助。

<body>
    <div class="container mt-3">
        <table class="table">
            <thead>
                <th>Sno</th>
                <th>name</th>
                <th>Roll No</th>
                <th>Section</th>
                <th>Gender</th>
            </thead>
            <tbody id="tbody1"></tbody>
        </table>
    </div>    

    <script type="module"> 
    
        function AddItemToTable(name,roll,sec,gen){
            let trow = document.createElement("throw");
            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');

            td1.innerHTML = ++stdNo;
            td2.innerHTML = name;
            td2.innerHTML = roll;
            td2.innerHTML = sec;
            td2.innerHTML = gen;

            trow.appendChild(td1);
            trow.appendChild(td2);
            trow.appendChild(td3);
            trow.appendChild(td4);
            trow.appendChild(td5);

            tbody.appendChild(trow);
        }

        function AddAllItemsToTable(TheStudent){
            stdNo=0;
            tbody.innerHTML="";
            TheStudent.forEach(element => {
                    AddAllItemsToTable(element.bayi, element.hak, element.har, element.tar)
            });
        }


    import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js";
    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries

    // Your web app's Firebase configuration
    const firebaseConfig = {
        apiKey: "AIzaSyDlckDXHnQLTqJYJXnJGwVxB-y80qGdcUA",
        authDomain: "realtimedb-ae1d5.firebaseapp.com",
        projectId: "realtimedb-ae1d5",
        storageBucket: "realtimedb-ae1d5.appspot.com",
        messagingSenderId: "682240595734",
        appId: "1:682240595734:web:998323d8e109cf10fae237"
    };




    // Initialize Firebase
    const app = initializeApp(firebaseConfig);

    async function GetAllDataRealTime(){

        const dbRef = collection (db,"Deneme");

        onSnapshpt(dbRef,(querySnapshot)=> {
            var students = [];

            querySnapshot.forEach(doc => {
            students.push(doc.data());
        });
        AddAllItemsToTable(students);

        })
}
    
        window.onload = GetAllDataRealTime;
    
    
    </script>
</body>
4

0 回答 0