该错误通常是因为您的Firestore规则不允许您的代码访问该数据集。要解决此问题,您需要在Firestore控制台中更新规则以允许访问或在Angular应用程序中使用受信任的身份验证来访问数据。
下面是一个使用身份验证和规则来解决此问题的代码示例:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// 允许读取和写入
match /collectionName/{document=**} {
allow read, write: if true;
}
}
}
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MyService {
private collectionName: string = 'collectionName'; // 集合名称
constructor(private firestore: AngularFirestore) {
}
// 获取数据集
public getDataSet(): Observable {
const collectionRef: AngularFirestoreCollection = this.firestore.collection(this.collectionName);
return collectionRef.snapshotChanges().pipe(
map((actions: any[]) => actions.map((a: any) => ({ id: a.payload.doc.id, ...a.payload.doc.data() })))
);
}
}