Rxjs 如何使用@ngrx DB在集合中保存单个项目

Rxjs 如何使用@ngrx DB在集合中保存单个项目,rxjs,observable,ngrx,ngrx-effects,Rxjs,Observable,Ngrx,Ngrx Effects,我有以下@ngrx效果,当搜索完成时触发。SEARCH\u COMPLETE事件发出产品集合 我希望将集合中的每个产品保存到IndexedDB,而不是将整个集合作为单个值保存。为了实现这一点,我从使用@ngrxDB Observable的产品中创建了一个新的Observable。我不得不使用subscribe调用来使它工作 我的问题是,这样做对吗?还是不使用subscribe方法更好 @Effect() addProductsToDB$: Observable<Action> = t

我有以下
@ngrx
效果,当搜索完成时触发。
SEARCH\u COMPLETE
事件发出产品集合

我希望将集合中的每个产品保存到IndexedDB,而不是将整个集合作为单个值保存。为了实现这一点,我从使用
@ngrx
DB Observable的产品中创建了一个新的
Observable
。我不得不使用
subscribe
调用来使它工作

我的问题是,这样做对吗?还是不使用
subscribe
方法更好

@Effect()
addProductsToDB$: Observable<Action> = this.actions$
    .ofType(ProductsSearchActions.ActionTypes.SEARCH_COMPLETE)
    .map((action: ProductsSearchActions.SearchCompleteAction) => action.payload)
    .switchMap(products => {
        return Observable.from(products as IProduct[])
            .do(product => {
                this.db.insert('products', [{ product }])
                    .subscribe();
            })
            .map(() => new ProductDBActions.ProductAddedSuccessAction(true));
    });
@Effect()
addProductsToDB$:Observable=this.actions$
.of类型(ProductsSearchions.ActionTypes.SEARCH\u COMPLETE)
.map((操作:ProductsSearchActions.SearchCompleteAction)=>action.payload)
.switchMap(产品=>{
可观察的返回。来自(作为IPProduct[]的产品)
.do(产品=>{
this.db.insert('products',[{product}])
.subscribe();
})
.map(()=>newproductdbactions.ProductAddedSuccessAction(true));
});
试着去做

 Observable.from(products as IProduct[])
  .switchMap(product=> this.db.insert('products', [{ product }]))
  .map(() => new ProductDBActions.ProductAddedSuccessAction(true));

尝试执行“Observable.from(产品为IPProduct[]).switchMap(产品=>this.db.insert('products',[{product}])).map(()=>newproductdbactions.ProductAddedSuccessAction(true));我尝试了你的建议,效果很好。如果你能在下面创建一个答案,我可以将你的回答标记为答案。我可以问你为什么没有
subscribe
呼叫它就可以工作吗?@wonderfulworld它可以工作,因为
switchMap
包含一个隐式
subscribe