Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that, if I understand the core question, the short answer is that there is no way to force an empty array into Firebase. However, there are some paradigms that might work better than what you have above.</p> <p>Keep in mind that Firebase is a real-time environment. The number of cars and accidents can (and will) change at any time. It's best to treat everything as new data arriving in real time and avoid even thinking about exists or doesn't exist.</p> <pre><code>// fetch all the people in real-time rootRef.child('people').on('child_added', function(personSnapshot) { // monitor their cars personSnapshot.ref().child('cars', 'child_added', function(carSnapshot) { // monitor accidents carSnapshot.ref().child('accidents', 'child_added', function(accidentSnapshot) { // here is where you invoke your code related to accidents }); }); }); </code></pre> <p>Note how there is no need for <code>if exists/unless</code> type logic. Note that you would probably also want to monitor <code>child_removed</code> on <code>cars</code> and <code>people</code> and call <code>ref.off()</code> to stop listening to specific children.</p> <p>If for some reason you want to stick with the static model, then <a href="https://www.firebase.com/docs/javascript/datasnapshot/foreach.html" rel="noreferrer">forEach</a> will become your friend:</p> <pre><code>// fetch all the people as one object, asynchronously // this won't work well with many thousands of records rootRef.child('people').once('value', function(everyoneSnap) { // get each user (this is synchronous!) everyoneSnap.forEach(function(personSnap) { // get all cars (this is asynchronous) personSnap.ref().child('cars').once('value', function(allCars) { // iterate cars (this is synchronous) allCars.forEach(function(carSnap) { /* and so on */ }); }); }); }); </code></pre> <p>Note how, even with forEach, there is no need for "exists or unless" sort of logic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload