Javascript 财产';onSnapshot';不存在于类型';AngularFirestoreCollection<;未知>';
我正在尝试将侦听器从CloudFirebase中分离,但仍然出现此错误 类型“AngularFirestoreCollection”上不存在属性“onSnapshot” 你知道我应该进口什么吗?我直接从Firebase文档中复制此代码Javascript 财产';onSnapshot';不存在于类型';AngularFirestoreCollection<;未知>';,javascript,firebase,google-cloud-firestore,angularfire2,Javascript,Firebase,Google Cloud Firestore,Angularfire2,我正在尝试将侦听器从CloudFirebase中分离,但仍然出现此错误 类型“AngularFirestoreCollection”上不存在属性“onSnapshot” 你知道我应该进口什么吗?我直接从Firebase文档中复制此代码 let unsub = this.firestore.collection('cities').onSnapshot(() => { }); // Stop listening for changes unsub(); 这是我的构造函数和导入: impo
let unsub = this.firestore.collection('cities').onSnapshot(() => {
});
// Stop listening for changes
unsub();
这是我的构造函数和导入:
import { AngularFirestore } from "@angular/fire/firestore";
constructor(public firestore: AngularFirestore) {}
谢谢你的建议
AngularFirestore
是库中的一个类angularfire
,根据源代码,该类AngularFirestore
不包含任何名为onSnapshot()
的方法
在这种情况下,db
等于firebase.firestore()代码>
如果要使用AngularFire,则可以使用snpashotChanges()
:
this.shirtCollection=afs.collection('shirts');
//.snapshotChanges()返回DocumentChangeAction[],其中包含
//关于每一次更改“发生了什么”的大量信息。如果你想
//使用map操作符获取数据和id。
this.shirts=this.shirtCollection.snapshotChanges().pipe(
map(actions=>actions.map(a=>{
const data=a.payload.doc.data(),如图所示;
const id=a.payload.doc.id;
返回{id,…data};
}))
);
db
等于什么?哦,对不起,它应该是“firestore”-在构造函数(编辑)中,你能告诉我,如何导入或如何使用这个纯JS库吗?请遵循这个,我正试图通过文档来设置它,但只使用node.JS或纯javascript,结合angular,这不起作用…属性“map”在类型“unknown”上不存在-当我将鼠标悬停在actions=>actions.map(a=>…)上时,很抱歉回复太晚,但您不必使用map
您可以在您的服务类中使用返回此.shirtCollection.snapshotChanges()
,然后在您的组件中订阅它
db.collection("cities").doc("SF")
.onSnapshot(function(doc) {
console.log("Current data: ", doc.data());
});
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 };
}))
);