“AngularFirestoreCollection<unknown>"类型上不存在属性“onSnapshot" [英] Property 'onSnapshot' does not exist on type 'AngularFirestoreCollection<unknown>'

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

问题描述

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

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

AngularFirestoreCollection"类型不存在onSnapshot"属性

Property 'onSnapshot' does not exist on type 'AngularFirestoreCollection'

你知道吗,我要导入什么?我直接从 Firebase 文档中复制了这段代码.

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

感谢您的建议!

推荐答案

AngularFirestoreangularfire 库中的一个类,根据源码https://github.com/angular/angularfire/blob/master/src/firestore/firestore.ts#L106AngularFirestore 类不包含任何名为 onSnapshot() 的方法.

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-数据/听

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

在这种情况下 db 等于 firebase.firestore();

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

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

如果你想使用 AngularFire 那么你可以使用 snpashotChanges():

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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆