我正在努力获得新的getCountFromServer()
Firebase 9.11 中发布的功能可以与 Angularfire 一起使用和/或不与 Angularfire 一起使用,但我收到一个奇怪的错误。
案例:我想检索组织内成员集合的大小/长度 (/organizations/${orgId}/members),而不请求和读取所有成员文档。
Versions
"firebase": "^9.12.1",
"@angular/fire": "^7.4.1",
代码(简化):
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错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性(读取“wt”)
类型错误:无法读取未定义的属性(读取“wt”)
有人遇到过这种情况吗?有人可以帮忙吗? :)
OBS:我在其他 Angularfire 和 Firestore 调用中没有遇到任何错误。
您正在导入getCountFromServer()
来自模块化 SDK,但其余代码仍然使用compat
版本。最好不要同时使用这两个版本。另外,新COUNT()
函数似乎不属于compat
SDK所以没有.count()
方法与 NodeJS SDK 中的方法相同。
作为解决方法,您可以直接使用 Firebase JS SDK 来实现计数函数,如下所示:
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);
}
}
但是,我建议将代码升级到较新的版本模块化SDK https://github.com/angular/angularfire/tree/master/samples/modular.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)