2

I have the following search that I try to execute:
I want to search something based on the first id, this returns a FirebaseListObservable. I then try to execute code inside the switchMap function of the Observable because the search usually takes a bit longer and my code would be finished otherwise and the wrong values for lastLoc would be used.

I try to run this for a series of points so I loop through an array in the beginning.

Here is my code:

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

I receive the following error:

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<{}>'.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Spurious
  • 1,903
  • 5
  • 27
  • 53
  • 2
    You're not returning anything from the `switchMap` callback function. – martin Jul 26 '17 at 09:34
  • What would I need to return? I assumed I don't need to return anything as I am in an observable. – Spurious Jul 26 '17 at 09:37
  • FYI, the code worked fine when I used `map` instead of `switchMap`. – Spurious Jul 26 '17 at 09:38
  • 1
    The function passed to `map` can return any value - including `undefined` - the function passed to `switchMap` has to return an [`ObservableInput`](http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObservableInputDoc.html) and `undefined` is *not* a valid `ObservableInput`. – cartant Jul 26 '17 at 09:41
  • Do you have an idea how I solve this? This can't be difficult right? I saw in a tutorial that used it in a similar manner. The link says `ObservableInput interface describes all values that are either an SubscribableOrPromise or some kind of collection of values that can be transformed to Observable emitting that values. Every operator that accepts arguments annotated with this interface, can be also used with parameters that are not necessarily RxJS Observables.` I thought I passed a `FirebaseListObservable` and thus satisfied this criteria. – Spurious Jul 26 '17 at 09:49

2 Answers2

2

I solved my problem by adding the following to the end of the switchMap function:

import { Observable } from 'rxjs/Observable';
.switchMap(items => {
    ...
    return Observable.of(route);
  }).subscribe();
Spurious
  • 1,903
  • 5
  • 27
  • 53
0

You should use Observable's do method when you don't need to modify its stream.

Vadim Khamzin
  • 367
  • 1
  • 10
  • The problem with `do` is that it executes immediately while here I need to wait for the `FirebaseListObservable` to return a value. As there is a dependency, `do` will return the wrong values. – Spurious Jul 26 '17 at 09:53