I'm trying to get the new getCountFromServer()
feature released in Firebase 9.11 to work with Angularfire and/or without, but I get a strange error.
Case: I want to retrieve the size/length of the collection of members within an organization (/organizations/${orgId}/members), without requesting and reading all members docs.
Versions
"firebase": "^9.12.1",
"@angular/fire": "^7.4.1",
Code (Simplified):
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { getCountFromServer } from '@angular/fire/firestore';
@Component({
selector: 'test-member-size',
templateUrl: './test-member-size.component.html',
})
export class TestMemberSizeComponent implements OnInit {
constructor(private Firestore: AngularFirestore) { }
async ngOnInit() {
let collectionRef: any = this.Firestore.collection('/organizations/abc-org/members').ref
let snapshot = await getCountFromServer(collectionRef);
console.log('Test:')
console.log(snapshot.data().count)
}
}
Error:
core.js:6479 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'wt') TypeError: Cannot read properties of undefined (reading 'wt')
Have any encountered this and can anybody help? :) OBS: I do not encounter any errors on other Angularfire and Firestore calls.
CodePudding user response:
You are importing getCountFromServer()
from Modular SDK but rest of the code still uses compat
version. It's best to not use both the versions together. Also, the new COUNT()
function doesn't seem to be a part of compat
SDK so there is no .count()
method as in the NodeJS SDK.
As a workaround, you can use the Firebase JS SDK directly for the count function as shown below:
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { getCountFromServer, collection } from 'firebase/firestore';
export class TestMemberSizeComponent implements OnInit {
constructor(private Firestore: AngularFirestore) {}
async ngOnInit() {
const collectionRef = collection(this.Firestore.firestore, 'users');
const count = await getCountFromServer(collectionRef);
console.log(count.data().count);
}
}
However, I would recommend upgrading the code to the newer Modular SDK.