如何管理应用中的动态路径到Firebase数据 [英] How to manage dynamic path in app to Firebase data

查看:102
本文介绍了如何管理应用中的动态路径到Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase中的数据看起来像这样(这是一个基于Javascript的网络应用程序):

 出价
400
1
50.00
2
60.00
401
1
55.00
2
65.00

400和401指的是拍卖号码。 1和2指的是每个拍卖中的个别地段。第三级是出价金额。



我通过观看child_added来显示所有出价的列表:

  dataRef.child('bids')。child(auction).child(lotno).on('child_added',function(data){...} ); 

当应用第一次加载时,还没有分配拍卖或批号。随着拍卖的进展,批号(甚至可能是拍卖号码)将会改变很多次。

我很困惑如何保持一个实时列表出现这种情况的出价。当应用程序第一次加载时,我的变量auction和lotno的值为undefined,因此我从Firebase中得到了一个无效路径的错误。



如果一个变量(也就是路径)发生了变化,我也不确定我的child_added会发生什么。请问child_added在新路径中加载所有现有的数据,并从这一点开始注意新的孩子吗?



如何处理应用程序的初始状态,那里的变量没有被填充呢?

解决方案

由于您构建的查询是这样的:

  dataRef.child('bids')。child(auction).child(lotno).on(... 

$ b

您必须创建附加一个新的侦听器(并将关闭现有的侦听器)每当动作 lotno 变量中的一个发生变化时。这听起来像一次只能有一个当前批次,这可能会使它更有用于将其建模到数据模型中:

 出价
400
1
50.00
2
60.00
401
1
55.00
当前
然后,一旦当前批次的出价已经完成,您可以退休到适当的位置lotnumber插槽,并有下一批成为当前。有了这样的数据结构,您的拍卖倾听者可以始终观看 current

  dataRef.child('bids')。child(auction).child('current')。on(... 


I have data in my Firebase that looks something like this (this is a Javascript-based web app):

bids
    400
        1
            50.00
        2
            60.00
    401
        1
            55.00
        2
            65.00

400 and 401 refer to auction numbers. 1 and 2 refer to individual lots within each auction. And the 3rd level is a bid amount.

I am displaying a list of all bids to the user by watching for child_added like this:

dataRef.child('bids').child(auction).child(lotno).on('child_added', function(data){...});

When the app first loads, there is no auction or lot number assigned yet. And as the auction progresses, the lot number (and possibly even the auction number) will change many times.

I am confused as to how to maintain a real-time list of bids given this scenario. When the app first loads, my variables "auction" and "lotno" have the value "undefined", therefore I get an "invalid path" error from Firebase.

I'm also not sure what happens to my child_added if a variable (and therefore, the path) changes. Will child_added load all the existing data at the new path, and start watching for new children from that point forward?

Any advice on how to handle the initial state of the app, where the variables are not populated yet?

解决方案

Since you're building your query like this:

dataRef.child('bids').child(auction).child(lotno).on(...

You will have to create attach a new listener (and turn off the existing listener) every time one of the action or lotno variables changes.

It sounds like there can only be a single current lot at a time, which might make it more useful to model that into your data model:

bids
    400
        1
            50.00
        2
            60.00
    401
        1
            55.00
        current
            65.00

Then once the bidding on the current lot has finished, you can "retire" it to its proper lotnumber slot and have the next lot become the current. With a data structure like this, your listener for an auction could always be watching current:

dataRef.child('bids').child(auction).child('current').on(...

这篇关于如何管理应用中的动态路径到Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆