简体   繁体   English

可观察流上的地图未调用功能

[英]Map on Observable Stream Not Calling Function

I have an array of Observables that I create by pushing HTTP requests into an Observable array. 我有一个通过将HTTP请求推入Observable数组创建的Observable数组。

Once these requests are done, I forkJoin the collection of Observables and then from there, I'd like to then loop over this collection, perform some function on each item, and then finally, execute one last method. 完成这些请求后,我将forkJoin集合加入,然后从那里开始,然后遍历该集合,对每个项目执行一些功能,最后执行最后一个方法。

I have something similar to this: 我有类似的东西:

// Firstly, get all of the items
private _getItems(): Observable<MyItems[]> {
    let items: Observable<MyItems>[] = [];

    Object.keys(this._items).map(item => {
        items.push(this.service.getItemState(item))
    });

    return Observable.forkJoin(items);
}

// Secondly, loop through items and perform function
this._getItems()
    .map(items => items.map(item => {
        this._setState(item);
    }))
    // Finally, do my last method
    .subscribe(() => {
        this._prepareItems();
    });
}

I'm not sure of why, but this._setState(item) is never being called. 我不确定为什么,但是从未调用this._setState(item) Is this the right way to go about doing this? 这是执行此操作的正确方法吗?

Thanks 谢谢

At first forkJoin is a good choice since it waits for all of your http-requests to complete. 最初,forkJoin是一个不错的选择,因为它会等待所有http请求完成。 But im not sure if your forEach on the object is working? 但是我不确定对象上的forEach是否正常工作? Wouldn't it be like this: 会不会像这样:

Object.keys(this._items).map((key, index) => {
     items.push(this.service.getItemState(this._items[key]))
});

On your second example you create a new observable by using map with the forkJoin observable as source. 在第二个示例中,通过使用带有forkJoin observable作为源的map创建一个新的observable。 You use map but dont map anything so instead use tap() (was called do() on before RxJS 5.5) 您使用map但是不映射任何东西,因此使用tap() (在RxJS 5.5之前称为do()

I would highly recommend you to insert a console.log on nearly each line of your code and see if its working correctly. 我强烈建议您在代码的几乎每一行上插入console.log,并查看其是否正常运行。 Is the forEach on object working? forEach对象运行正常吗? Is _getItems() every emitting something? _getItems()是否都发出某种东西? (use do/tap for console.log) (对console.log使用do / tap)

Btw you should not call everything "item". 顺便说一句,您不应将所有内容都称为“项目”。 this._setState(item); is probably more something like this._setState(itemState); 可能更像这样this._setState(itemState); isnt it? 是不是

This should work, regardless of the version of Angular / RxJS that you are using. 无论您使用的是Angular / RxJS的版本如何,这都应该起作用。

 // Your code that isn't working: /*// Firstly, get all of the items private _getItems(): Observable<MyItems[]> { let items: Observable<MyItems>[] = []; Object.keys(this._items).map(item => { items.push(this.service.getItemState(item)) }); return Observable.forkJoin(items); } // Secondly, loop through items and perform function this._getItems() .map(items => items.map(item => { this._setState(item); })) // Finally, do my last method .subscribe(() => { this._prepareItems(); }); }*/ // Code rewritten (Angular 2-4 / RxJS 2-4): import { Observable } from 'rxjs'; import 'rxjs/add/operator/map'; private _getItems(): Observable<MyItems[]> { return Observable.forkJoin( Object.keys(this._items) .map(item => this.service.getItemState(item)) ); } this._getItems() .map(items => items.map(item => { this._setState(item); return item; })) .subscribe( item => this._prepareItems(), err => console.error(err) ); // Code rewritten (Angular 5+ / RxJS 5+): import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; private _getItems(): Observable<MyItems[]> { return Observable.combineLatest( Object.keys(this._items) .map(item => this.service.getItemState(item)) ); } this._getItems() .pipe( tap(items => items.map(item => this._setState(item))) ) .subscribe( item => this._prepareItems(), err => console.error(err) ); 

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

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