Reputation: 705
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?
Upvotes: 2
Views: 1452
Reputation: 599816
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(...
Upvotes: 5