Home > @firebase/database > DataSnapshot > forEach
DataSnapshot.forEach() method
Enumerates the top-level children in the DataSnapshot
.
Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned by val()
is not guaranteed to match the ordering on the server nor the ordering of onChildAdded()
events. That is where forEach()
comes in handy. It guarantees the children of a DataSnapshot
will be iterated in their query order.
If no explicit orderBy*()
method is used, results are returned ordered by key (unless priorities are used, in which case, results are returned by priority).
Signature:
forEach(action: (child: DataSnapshot) => boolean | void): boolean;
Parameters
Parameter | Type | Description |
---|---|---|
action | (child: DataSnapshot) => boolean | void | A function that will be called for each child DataSnapshot. The callback can return true to cancel further enumeration. |
Returns:
boolean
true if enumeration was canceled due to your callback returning true.