Angular 角度6可观测返回

Angular 角度6可观测返回,angular,rxjs,observable,Angular,Rxjs,Observable,我不熟悉AngularJs(使用AngularJs已有多年),我正在与可观察物作斗争:( 我有这个方法: filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> { var formulas = this.formulaService.getFromSelectedAnswers(questions); let shouldFilter

我不熟悉AngularJs(使用AngularJs已有多年),我正在与可观察物作斗争:( 我有这个方法:

filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
  var formulas = this.formulaService.getFromSelectedAnswers(questions);
  let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
  if (!shouldFilter) return;

  return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
    if (!products || !products.length) return;

    this.products.length = 0;
    this.products.push.apply(this.products, products);
    this.questionService.filterQuestionsByProducts(products, questions);      
    this.questionService.updateSession(questions);
  }));
}
filterProducts(categoryId:string,questions:Question[],OrganizationId:string):可观察{
var formulas=this.formulaService.getFromSelectedAnswers(问题);
让shouldFilter=this.shouldFilterProducts(类别ID、问题、公式);
如果(!shouldFilter)返回;
返回此.filterOrScore(类别ID、组织ID、公式)。管道(映射(产品=>{
如果(!products | |!products.length)返回;
this.products.length=0;
本.产品.推送.应用(本.产品,产品);
this.questionService.filterQuestionsByProducts(产品,问题);
this.questionService.updateSession(问题);
}));
}
如果(!shouldFilter)返回,行
,则该行不正确。我需要它返回一个可观察值,以便我的订阅工作。

有人知道怎么做吗?

如果您不关心订阅中的返回值,可以返回空值。这将完成可观察的

import {empty} from 'rxjs';

filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
  var formulas = this.formulaService.getFromSelectedAnswers(questions);
  let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
  if (!shouldFilter) return empty();

  return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
    if (!products || !products.length) return empty();

    this.products.length = 0;
    this.products.push.apply(this.products, products);
    this.questionService.filterQuestionsByProducts(products, questions);      
    this.questionService.updateSession(questions);
  }));
从'rxjs'导入{empty};
filterProducts(categoryId:string,questions:Question[],OrganizationId:string):可观察{
var formulas=this.formulaService.getFromSelectedAnswers(问题);
让shouldFilter=this.shouldFilterProducts(类别ID、问题、公式);
如果(!shouldFilter)返回空();
返回此.filterOrScore(类别ID、组织ID、公式)。管道(映射(产品=>{
如果(!products | |!products.length)返回空();
this.products.length=0;
本.产品.推送.应用(本.产品,产品);
this.questionService.filterQuestionsByProducts(产品,问题);
this.questionService.updateSession(问题);
}));

如果您不关心订阅中的返回值,则可以返回空。这将完成可观察的

import {empty} from 'rxjs';

filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
  var formulas = this.formulaService.getFromSelectedAnswers(questions);
  let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
  if (!shouldFilter) return empty();

  return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
    if (!products || !products.length) return empty();

    this.products.length = 0;
    this.products.push.apply(this.products, products);
    this.questionService.filterQuestionsByProducts(products, questions);      
    this.questionService.updateSession(questions);
  }));
从'rxjs'导入{empty};
filterProducts(categoryId:string,questions:Question[],OrganizationId:string):可观察{
var formulas=this.formulaService.getFromSelectedAnswers(问题);
让shouldFilter=this.shouldFilterProducts(类别ID、问题、公式);
如果(!shouldFilter)返回空();
返回此.filterOrScore(类别ID、组织ID、公式)。管道(映射(产品=>{
如果(!products | |!products.length)返回空();
this.products.length=0;
本.产品.推送.应用(本.产品,产品);
this.questionService.filterQuestionsByProducts(产品,问题);
this.questionService.updateSession(问题);
}));