Xerri
Xerri

Reputation: 5046

Get length of object in angularfire2 #askfirebase

Is there a way to use angularfire2 and get the length of an object?

Upvotes: 0

Views: 574

Answers (1)

Calvin Ferrando
Calvin Ferrando

Reputation: 4082

Try this one:

import { FirebaseApp } from "angularfire2";
import { Inject } from "@angular/core";

export class AppComponent {
    constructor(@Inject(FirebaseApp) fb: any) {
        const ref = fb.database().ref();
        ref.child('/lists')
            .once('value')
            .then(
                (snapshot) => {
                    console.log(snapshot.numChildren()); // gets length
                }
            );
    }
}

or

import { AngularFireDatabase } from "angularfire2";

export class AppComponent {
    constructor(private afd: AngularFireDatabase) {
        const lists = afd.object(`/lists`, { preserveSnapshot: true });
        lists.subscribe(snapshot => {
            console.log(snapshot.numChildren()); // gets length
        });
    }
}

Upvotes: 2

Related Questions