本文介绍了类型"AngularFirestoreCollection< unknown>"上不存在属性"onSnapshot"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Cloud Firebase分离侦听器,但是仍然出现此错误.

您知道吗,我要输入什么?我直接从Firebase文档中复制此代码.

let unsub = this.firestore.collection('cities').onSnapshot(() => {
});

// Stop listening for changes
unsub();

这是我的构造函数并导入:

import { AngularFirestore } from "@angular/fire/firestore";

constructor(public firestore: AngularFirestore) {}

感谢您的建议!

根据源代码 https://github.com/angular/angularfire/blob/master/src/firestore/firestore.ts#L106 ,类AngularFirestore不包含任何称为onSnapshot()的方法.

https://firebase.google.com/docs/firestore/query -数据/收听

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 };
      }))
    );

I am trying to detach listener from cloud Firebase, but I still get this error.

Do you know, what sould I import? I directly copy this code from Firebase documentation.

let unsub = this.firestore.collection('cities').onSnapshot(() => {
});

// Stop listening for changes
unsub();

This is my constructor and import:

import { AngularFirestore } from "@angular/fire/firestore";

constructor(public firestore: AngularFirestore) {}

Thanks for your advices!

解决方案

AngularFirestore is a class in the library angularfire, according to the source code https://github.com/angular/angularfire/blob/master/src/firestore/firestore.ts#L106, the class AngularFirestore does not contain any method called onSnapshot().

https://firebase.google.com/docs/firestore/query-data/listen

db.collection("cities").doc("SF")
    .onSnapshot(function(doc) {
        console.log("Current data: ", doc.data());
    });

In this case db is equal to firebase.firestore();

https://firebase.google.com/docs/firestore/quickstart

If you want to use AngularFire then you can use 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 };
      }))
    );

这篇关于类型"AngularFirestoreCollection&lt; unknown&gt;"上不存在属性"onSnapshot"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 02:37
查看更多