简体   繁体   English

为什么 AngularFire 不返回我的 Firestore 集合中的最新数据?

[英]Why doesn't AngularFire return the latest data in my Firestore collection?

So I'm trying to build a real-time app to scrape data in the background (using Functions) and to display it on an Angular app.所以我正在尝试构建一个实时应用程序来在后台抓取数据(使用函数)并将其显示在 Angular 应用程序上。 The Functions are working as expected and update my Firestore collection throughout the day.这些功能按预期工作并全天更新我的 Firestore 集合。 Getting the latest data to my frontend is the problem.将最新数据获取到我的前端是问题所在。

I started off by subscribing to valueChanges on a filtered collection.我首先订阅过滤集合的valueChanges This works great to get the initial data, but it seems like changes aren't detected and the UI never updates with the new data.这对于获取初始数据非常有效,但似乎未检测到更改,并且 UI 永远不会使用新数据进行更新。

getRunners(raceUid: string, date: Date): Observable<Runner[]> {
  return this.firestore
    .collection<Runner>(Collections.Runners, (ref) =>
      ref.where('raceUid', '==', raceUid)
        .where('created', '>=', date)
        .orderBy('created', 'asc')
        .orderBy('number', 'asc'),
    )
    .valueChanges();
}

After that I tried polling for the updated data by subscribing to the same valueChanges observable on a 10 second interval.之后,我尝试通过订阅以 10 秒为间隔观察到的相同valueChanges来轮询更新的数据。 But still the UI does not get updated with new data, even though my subscribe callback is definitely running.但是 UI 仍然没有更新新数据,即使我的订阅回调确实在运行。

I know the changes are there when I request it, because when I refresh the page all the changes display.当我请求它时,我知道更改就在那里,因为当我刷新页面时,所有更改都会显示。 So the initial call gets the latest data but it seems after that it just uses local cache maybe and never retrieves live data?所以初始调用获取最新数据但似乎之后它可能只使用本地缓存并且永远不会检索实时数据? Is that possible?那可能吗?

I'm using AngularFire 7.4.1 and I don't explicitly enable or disable persistence in my app.module .我正在使用 AngularFire 7.4.1 并且我没有在我的app.module中明确启用或禁用持久性。

EDIT: Here is my code that calls the getRunners method.编辑:这是我调用getRunners方法的代码。 This one is for polling:这个用于投票:

this.updateInterval$
      .pipe(
        switchMap(() =>
          this.runnerService.getRunners(raceUid, new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate(), 0, 0, 0)),
        ),
        takeUntil(this.raceChange$),
      )
      .subscribe((runners) => {
        this.runners = runners;
        this.lastUpdated = new Date();
      });

And here is what I started with initially:这是我最初的开始:

this.runnerService
      .getRunners(raceUid, new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate(), 0, 0, 0))
      .pipe(takeUntil(this.destroy$))
      .subscribe((runners) => {
        this.blockUi.stop();
        this.runners = runners;
        this.lastUpdated = new Date();
      });

Map the Runners snapshot changes to the require type using .pipe and map method like I have shown below:使用.pipemap方法将 Runners 快照更改映射到所需类型,如下所示:

In Service:在职:

getRunners(raceUid: string, date: Date): Observable<Runner[]> {
  return this.firestore
    .collection<Runner>(Collections.Runners, (ref) =>
      ref.where('raceUid', '==', raceUid)
        .where('created', '>=', date)
        .orderBy('created', 'asc')
        .orderBy('number', 'asc'),
    )
    .snapshotChanges()
    .pipe(
      map((snapshots) => {
        return snapshots.map((snapshot) => {
          const data = snapshot.payload.doc.data();
          const id = snapshot.payload.doc.id;
          const updateTime = snapshot.payload.doc.updateTime;
          return { id, updateTime, ...data };
        });
      }),
    );
}

In Component we are getting the runners object with type safety involved then on subscribing you can implement your logic about runners.在 Component 中,我们获取涉及类型安全的 runners 对象,然后在订阅时您可以实现有关 runners 的逻辑。

this.runnerService
  .getRunners(raceUid, new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate(), 0, 0, 0))
   .pipe(takeUntil(this.destroy$))
  .subscribe((runners) => {
        this.blockUi.stop();
        this.runners = runners;
        this.lastUpdated = new Date();
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM