简体   繁体   中英

How do I iterate through all child nodes in firebase database using javascript?

Below is how my firebase database is structured. I would like to iterate through all the nodes named details that are under the residents node and display the value stored under the name, say example 'first_name'. Assistance would be grateful.

{
  "nibebe-530edaddclose":{
    "Estate":{
        "WVLN5yN88oZSzgIl7JpfbiRadYq1":{
            "details":{},
            "residents":{
                "details":{
                    "-LsxA0DMx5EqUe6xSvvM":{
                        "block_number":, 
                        "email":, 
                        "first_name":, 
                        "house_number":, 
                        "last_name":, 
                        "password":, 
                        "phone_number":,
                    }
                }
            }
        }
        "XxWh0j0QYIUZ35chQqkemwnB3Mw1":{
            "details":{},
            "residents":{
                "details":{
                    "-LsuDQO699osHfqq5BIC":{
                        "block_number":, 
                        "email":, 
                        "first_name":, 
                        "house_number":, 
                        "last_name":, 
                        "password":, 
                        "phone_number":,
                    }
                }
            }
        } 
    } 
} 

}`

Thanks in advance

Do you mean, in this example, you would want to get the value of both first_name fields? There is unfortunately no trick to this, and no good way to do it. Firebase does not support querying and (in my experience) is not well-suited to this kind of use case. You will need to get a ref ( https://firebase.google.com/docs/reference/js/firebase.database.Reference ) to the first shared ancestor of these objects (in this case, Estate ) and manually iterate through them. Depending on the size of your data, this might be a lengthy operation, so you might need to consider batching, etc. in order to prevent this operation from blocking something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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