我正在尝试从Cloud Firebase分离侦听器,但是仍然出现此错误。
类型“ AngularFirestoreCollection”上不存在属性“ onSnapshot”
您知道吗,我要输入什么?我直接从Firebase文档中复制此代码。
let unsub = this.firestore.collection('cities').onSnapshot(() => {
});
// Stop listening for changes
unsub();
这是我的构造函数并导入:
import { AngularFirestore } from "@angular/fire/firestore";
constructor(public firestore: AngularFirestore) {}
感谢您的建议!
答案 0 :(得分:1)
AngularFirestore
是库angularfire
中的类,根据源代码https://github.com/angular/angularfire/blob/master/src/firestore/firestore.ts#L106,类AngularFirestore
不包含任何称为onSnapshot()
的方法。
https://firebase.google.com/docs/firestore/query-data/listen
db.collection("cities").doc("SF")
.onSnapshot(function(doc) {
console.log("Current data: ", doc.data());
});
在这种情况下,db
等于firebase.firestore();
https://firebase.google.com/docs/firestore/quickstart
如果要使用AngularFire,则可以使用snpashotChanges()
:
this.shirtCollection = afs.collection<Shirt>('shirts');
// .snapshotChanges() returns a DocumentChangeAction[], which contains
// a lot of information about "what happened" with each change. If you want to
// get the data and the id use the map operator.
this.shirts = this.shirtCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
}))
);