简体   繁体   English

如何从多层多个子节点内的 Firebase 数据库中检索数据?

[英]How to retrieve the data from Firebase Database inside muti-layer multiple child nodes?

我的数据库快照

I have been trying to retrieve the data from Firebase Database but unable to find a way with this type of data structure.我一直在尝试从 Firebase 数据库中检索数据,但无法找到使用此类数据结构的方法。 As you can see in the AllStores node, there are multiple stores and each store has its own data with own products and the products have many main categories and each main category has multiple sub category , sub- categories have multiple brands and finally each brand has multiple products with a random number as its node.正如你在 AllStores 节点中看到的,有多个商店,每个商店都有自己的数据和自己的产品,产品有很多主类别,每个主类别有多个子类别,子类别有多个品牌,最后每个品牌都有以随机数为节点的多个产品。 I know this structure is a bit complicated and i can get the data by separating the nodes and create again and again which is a bit tedious.我知道这个结构有点复杂,我可以通过分离节点来获取数据并一次又一次地创建,这有点乏味。 So, is the any possible way of retrieving the whole store with all of its data in an single object and put it on a list or do i have to manually put the path by just retrieving the store data and use reference.child("") method to get each and every data.那么,是否有任何可能的方法来检索整个商店及其所有数据在单个对象中并将其放在列表中,或者我是否必须通过仅检索商店数据并使用 reference.child("" ) 方法来获取每个数据。 If i find a way of retrieving this data structure without changing its nodes , it will be of great ease to me .如果我找到了一种无需更改节点即可检索此数据结构的方法,那对我来说将非常容易。 Any answer is much appreciated.任何答案都非常感谢。

is the any possible way of retrieving the whole store with all of its data in an single object是在单个对象中检索整个存储及其所有数据的任何可能方法

Yes, this is already what happens when you fetch a node from Realtime Database.是的,当您从实时数据库中获取节点时,就会发生这种情况。 When you get DataSnapshot for any given node, it will contain all of the child nodes inside it.当您为任何给定节点获取DataSnapshot 时,它将包含其中的所有子节点。 All you have to do is use child() on that snapshot to dig into each of the child nodes to get a new DataSnapshot with all of the child data.您所要做的就是在该快照上使用child()来挖掘每个子节点,以获取包含所有子数据的新 DataSnapshot。 Keep digging in to each child the same way.继续以同样的方式深入了解每个孩子。 Everything child will be nested inside other DataSnapshots.所有子项都将嵌套在其他 DataSnapshots 中。

So, if you have a DataSnapshot of "/AllStores/Mazbat Store", you can go down as far as you need to find a snapshot with the data you want.因此,如果您有“/AllStores/Mazbat Store”的 DataSnapshot,您可以根据需要查找包含所需数据的快照。

DataSnapshot store = // fetch of "/AllStores/Mazbat Store"
DataSnapshot item = store.child("Store Products").child("Electronics").child("Camera").child("Asus").("6432649")

You will obviously have to write a lot of code to get a hold of everything from the top level snapshot, but it will all be there.显然,您必须编写大量代码才能从顶级快照中获取所有内容,但它们都会在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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