如何知道实时更新何时结束 - Cloud Firestore ionic 4 with angular
How to know when real time update ends - Cloud Firestore ionic 4 with angular
我正在为我的数据库使用 cloud firestore,读写没有问题。我的问题是我不知道如何知道实时更新何时结束!
我正在使用官方云 Firestore 文档中所述的实时更新。它 returns 是一个函数而不是一个可观察对象。现在我不确定如何正确使用它。
我需要在加载数据后执行一些代码,但我没有 subscribe((data) => {...}) 把它放在那里!!
怎么做?
如果我表现出缺乏知识,请指导我阅读一些文档。
谢谢
这段代码工作正常,我直接在 html 上使用它,比如 ListService.places$ 来访问数据数组。
public places$: BehaviorSubject<Array<Place>> = new BehaviorSubject<Array<Place>>([]);
private unsubscribe: Function;
public list(city: string, country: string) {
return this.unsubscribe = this.firestore.firestore.collection('places')
.where("location.country", "==", country)
.where("location.city", "==", city)
.orderBy("startDate", "desc")
.onSnapshot(querySnapshot => {
const list: Place[] = [];
if(!querySnapshot.docs.length) {
this.places$.next(list);
} else {
querySnapshot.docs.forEach(doc => {
let places = new Place();
place = doc.data() as Place;
places.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
});
}
});
}
您需要订阅this.places$
this.places$.subscribe((data) => {
console.log(data);
});
实时函数returns没有什么意义,因为实时函数将输出添加到BehaviorSubject
。
看这里
let places = new Place();
place = doc.data() as Place;
places.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
这意味着,如果您想对数据执行某些操作,只需订阅 places$
。
另外我认为你有一个小错字,你从未将值推送到 list
数组。
它应该是这样的:
...
.onSnapshot(querySnapshot => {
const list: Place[] = [];
if(!querySnapshot.docs.length) {
this.places$.next(list);
} else {
querySnapshot.docs.forEach(doc => {
let place = new Place();
place = doc.data() as Place;
list.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
});
}
});
我正在为我的数据库使用 cloud firestore,读写没有问题。我的问题是我不知道如何知道实时更新何时结束! 我正在使用官方云 Firestore 文档中所述的实时更新。它 returns 是一个函数而不是一个可观察对象。现在我不确定如何正确使用它。 我需要在加载数据后执行一些代码,但我没有 subscribe((data) => {...}) 把它放在那里!! 怎么做? 如果我表现出缺乏知识,请指导我阅读一些文档。 谢谢
这段代码工作正常,我直接在 html 上使用它,比如 ListService.places$ 来访问数据数组。
public places$: BehaviorSubject<Array<Place>> = new BehaviorSubject<Array<Place>>([]);
private unsubscribe: Function;
public list(city: string, country: string) {
return this.unsubscribe = this.firestore.firestore.collection('places')
.where("location.country", "==", country)
.where("location.city", "==", city)
.orderBy("startDate", "desc")
.onSnapshot(querySnapshot => {
const list: Place[] = [];
if(!querySnapshot.docs.length) {
this.places$.next(list);
} else {
querySnapshot.docs.forEach(doc => {
let places = new Place();
place = doc.data() as Place;
places.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
});
}
});
}
您需要订阅this.places$
this.places$.subscribe((data) => {
console.log(data);
});
实时函数returns没有什么意义,因为实时函数将输出添加到BehaviorSubject
。
看这里
let places = new Place();
place = doc.data() as Place;
places.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
这意味着,如果您想对数据执行某些操作,只需订阅 places$
。
另外我认为你有一个小错字,你从未将值推送到 list
数组。
它应该是这样的:
...
.onSnapshot(querySnapshot => {
const list: Place[] = [];
if(!querySnapshot.docs.length) {
this.places$.next(list);
} else {
querySnapshot.docs.forEach(doc => {
let place = new Place();
place = doc.data() as Place;
list.push(place);
if(list.length === querySnapshot.docs.length) {
this.places$.next(list);
}
});
}
});