最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get Firebase child nodes' names without getting their children too in Firebase response? - Stack Overflow

programmeradmin3浏览0评论

I have the following hierarchy on firebase, some data are hidden for confidentiality:

I'm trying to get a list of videos IDs (underlines in red)

I only can get all nodes, then detect their names and store them in an array!
But this causes low performance; because the dataSnapshot from firebase is very big in my case, so I want to avoid retrieving all the nodes' content then loop over them to get IDs, I need to just retrieve the IDs only, i.e. without their nested elements.

Here's my code:

new Firebase("/").once(
    'value', 
    function(dataSnapshot){ 

        // dataSnapshot now contains all the videos ids, lines & links
        // this causes many performance issues

        // Then I need to loop over all elements to extract ids !
        var videoIdIndex = 0;
        var videoIds = new Array();

        dataSnapshot.forEach(
            function(childSnapshot) {
                videoIds[videoIdIndex++] = childSnapshot.name();
            }
        );

    }
);

How may I retrieve only IDs to avoid lot of data transfer and to avoid looping over retrived data to get IDs ? is there a way to just retrive these IDs directly ?

I have the following hierarchy on firebase, some data are hidden for confidentiality:

I'm trying to get a list of videos IDs (underlines in red)

I only can get all nodes, then detect their names and store them in an array!
But this causes low performance; because the dataSnapshot from firebase is very big in my case, so I want to avoid retrieving all the nodes' content then loop over them to get IDs, I need to just retrieve the IDs only, i.e. without their nested elements.

Here's my code:

new Firebase("https://PRIVATE_NAME.firebaseio.com/videos/").once(
    'value', 
    function(dataSnapshot){ 

        // dataSnapshot now contains all the videos ids, lines & links
        // this causes many performance issues

        // Then I need to loop over all elements to extract ids !
        var videoIdIndex = 0;
        var videoIds = new Array();

        dataSnapshot.forEach(
            function(childSnapshot) {
                videoIds[videoIdIndex++] = childSnapshot.name();
            }
        );

    }
);

How may I retrieve only IDs to avoid lot of data transfer and to avoid looping over retrived data to get IDs ? is there a way to just retrive these IDs directly ?

Share Improve this question edited Jun 20, 2013 at 11:18 Ashraf Bashir asked Jun 20, 2013 at 10:54 Ashraf BashirAshraf Bashir 9,80415 gold badges60 silver badges83 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

UPDATE: There is now a shallow command in the REST API that will fetch just the keys for a path. This has not been added to the SDKs yet.

In Firebase, you can't obtain a list of node names without retrieving the data underneath. Not yet anyways. The performance problems can be addressed with normalization.

Essentially, your goal is to split data into consumable chunks. Store your list of video keys, possible with a couple meta fields like title, etc, in one path, and store the bulk content somewhere else. For example:

/video_meta/id/link, title, ...
/video_lines/id/...

To learn more about denormalizing, check out this article: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

It is a bit old, and you probably already know, but in case someone else comes along. You can do this using REST api call, you only need to set the parameter shallow=true

here is the documentation

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论