【问题标题】:how can i retrieve multilevel data in firebase database --Webapp如何在 Firebase 数据库中检索多级数据--Webapp
【发布时间】:2019-09-10 18:48:08
【问题描述】:

我是 firebase 新手,我正在尝试创建一个壁纸应用。我想从看起来像这样的数据库中检索数据。

。但它不止两个层次。我想显示男孩组和女孩组的详细信息,但不显示他们的名字。我想这样展示

如果我不添加级别的男孩组和女孩组,我可以做我想做的事,但我需要这些作为类别。它是三个级别的值,这让我很困惑如何一次性检索男孩组的孩子和女孩组的孩子。 这是两级孩子的代码..请帮助我

var dbCategories = firebase.database().ref("categories");
    dbCategories.on("value",function(categories){`


        if (categories.exists()) {
            var categorieshtml = "";

            categories.forEach( function(category) {
                // statements

                var newCategory =category.val();
                console.log(category);
                categorieshtml += "<tr>";

                //for category name 
                categorieshtml += "<td>";
                categorieshtml += category.key;
                console.log(categorieshtml);
                categorieshtml += "</td>";

                //for category Description

                categorieshtml += "<td>";
                categorieshtml += category.val().desc;
                console.log(categorieshtml);

                categorieshtml += "</td>";              

                //for category Thumbnail

                categorieshtml += "<td> <img width='250' height='150' src='";
                categorieshtml += category.val().thumbnail;
                categorieshtml += "'/></td>";
                categorieshtml += "</tr>";

            }); 

            $("#categories").html(categorieshtml);
        }

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database web-applications


    【解决方案1】:

    你可以这样做

    const dbCatagoriesRef = firebase.database.ref('categories')
    dbCatagoriesRef.on('value', snapshot => {
        const dbCatagories = snapshot.val();
        const bodyGroupCategory = dbCatagories.bodygroup;
        const girlGroupCategory = dbCatagories.girlgroup;
    
        // es6
        Object.values(bodyGroupCategory).forEach(wallpapper) {
            // Do with what is needed with the wallpapper
            console.log(wallpapper.thubmnail)
        }
    
        // can also do the same to girlGroupCategory 
    })
    

    但是,尽管您这样做...您不应该这样做...您应该只监听最小树上的更改...监听值 - 或其他 Firebase 更改 - 在树的顶部(文档/对象)会给您的数据库带来更大的压力,并且在大量数据的情况下,这甚至会使您的整个 firebase 数据库陷入困境。

    【讨论】:

    • 您好,谢谢!我想在网页上显示这两个类别的详细信息。我应该删除子类别男孩组和女孩组以减少对 DB 的压力吗?
    • 是的,你应该有很多原因...... 1)你为firebase支付带宽,你不想定期加载不需要的数据2)在一个大文档上附加firebase有许多嵌套的孩子会导致您的应用程序性能变慢 3)如果您在文档的根目录上附加一个侦听器,并且那里有很多数据,那么您的所有应用程序的 firebase 可能会卡住https://firebase.google .com/docs/database/web/read-and-write 你可以在这里阅读firebase推荐
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2016-11-27
    • 1970-01-01
    • 2016-11-22
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多