javascript - switchMap 不接受在其中执行的函数

标签 javascript firebase rxjs angularfire2 switchmap

我尝试执行以下搜索:
我想根据第一个 id 搜索一些内容,这将返回 FirebaseListObservable 。然后我尝试执行 switchMap 内的代码Observable的功能因为搜索通常需要更长的时间,否则我的代码将完成,并且 lastLoc 的值错误。会被使用。

我尝试对一系列点运行它,因此我在开始时循环遍历一个数组。

这是我的代码:

evaluateRoutes() {
  this.currentTour.travelDistance = 0;
  this.currentTour.travelTime = 0;
  this.currentTour.workTime = 0;
  const locs = this.currentTour.locIds;
  let lastLoc = null;
  locs.forEach(loc => {
    console.log(lastLoc, loc);
    if (lastLoc !== null) {
      console.log('if durch', lastLoc, loc);
      this.afDb.list('routes', {
        query: {
          orderByChild: 'idStart',
          equalTo: loc
        }
      }).switchMap(items => {
        console.log('switchMap starts', lastLoc, loc);
        const filtered = items.filter(item => item.idEnd === lastLoc);
        const route = filtered[0];
        this.currentTour.routeIds.push(route.$key);
        this.currentTour.routes.push({
          idEnd: route.idEnd,
          idStart: route.idStart,
          travelDistance: route.travelDistance,
          travelTime: route.travelTime,
          tsCreated: route.tsCreated,
          userCreated: route.userCreated
        });
        this.currentTour.travelDistance = +0 + this.currentTour.travelDistance + route.travelDistance;
        this.currentTour.travelTime = +0 + this.currentTour.travelTime + route.travelTime;
      }).subscribe();
    }
    lastLoc = loc;
  });
}

我收到以下错误:

Argument of type '(items: any) => void' is not assignable to parameter of type '(value: any, index: number) => ObservableInput<{}>'.
  Type 'void' is not assignable to type 'ObservableInput<{}>'.

最佳答案

我通过将以下内容添加到 switchMap 函数的末尾来解决我的问题:

import { Observable } from 'rxjs/Observable';
.switchMap(items => {
    ...
    return Observable.of(route);
  }).subscribe();

关于javascript - switchMap 不接受在其中执行的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322934/

相关文章:

javascript - 如何根据输入有条件地隐藏输入表单的某些部分?

javascript - 如何在 Angular 4中过滤JSON数据

javascript - 如何在不请求通知权限的情况下获取 Firebase Cloud Messaging 的注册 token ?

firebase - 数据库触发firebase函数从URL下载图像并将其保存到存储

javascript - 可观察中的数据集未在模板中更新

javascript - 如何在不执行更改检测例程的情况下使用 'interval' Rxjs 函数?

javascript - 在其他循环内延迟循环

javascript - 在导航链接单击上触发 Bootstrap 模式

swift 火力地堡 : loop through data and only start the new loop if the old one has finished loading

angular - NgRx: 得到 'You provided ' undefined' where a stream is expected.'当调用有效的完整 Action 时