我尝试执行以下搜索:
我想根据第一个搜索某些内容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<{}>'.