我正在嘗試讓getCountFromServer()Firebase 9.11 中發布的新功能與 Angularfire 一起使用和/或不使用,但我遇到了一個奇怪的錯誤。案例:我想檢索組織 (/organizations/${orgId}/members) 內成員集合的大小/長度,而無需請求和閱讀所有成員檔案。
版本
"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)
}
}
錯誤:
core.js:6479 錯誤錯誤:未捕獲(在承諾中):TypeError:無法讀取未定義的屬性(讀取“wt”)TypeError:無法讀取未定義的屬性(讀取“wt”)
有沒有人遇到過這個,有人可以幫忙嗎?:) OBS:我在其他 Angularfire 和 Firestore 呼叫中沒有遇到任何錯誤。
uj5u.com熱心網友回復:
您正在getCountFromServer()從 Modular SDK 匯入,但其余代碼仍使用compat版本。最好不要同時使用這兩個版本。此外,新COUNT()功能似乎不是compatSDK 的一部分,因此沒有.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);
}
}
但是,我建議將代碼升級到更新的Modular SDK。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/518684.html
