Arrays 为从另一个服务中找到的每个元素调用服务,然后用结果填充数组

Arrays 为从另一个服务中找到的每个元素调用服务,然后用结果填充数组,arrays,angular,service,Arrays,Angular,Service,问题: 尝试用两个服务填充html表。首先,我调用一个服务来检索所有产品。然后,对于每个元素,我调用另一个服务,该服务接受fabricplanid作为参数,并返回对象(如果存在)。如果存在,我将其推送到一个数组,但如果返回错误404,我将字符串推送到同一个数组。 我面临的问题是数组中的值与产品对应的fabricPlanId不匹配 这是product.component.ts文件,调用该文件时执行此服务并使用ngfor填充表 products:Product[]; //view fabr

问题: 尝试用两个服务填充html表。首先,我调用一个服务来检索所有产品。然后,对于每个元素,我调用另一个服务,该服务接受
fabricplanid
作为参数,并返回对象(如果存在)。如果存在,我将其推送到一个数组,但如果返回错误404,我将字符串推送到同一个数组。
我面临的问题是数组中的值与产品对应的
fabricPlanId
不匹配

这是product.component.ts文件,调用该文件时执行此服务并使用ngfor填充表

products:Product[];

  //view
  fabricplan: Fabricplan[];
  plan_desc: Array<String> = [];

  //view select
  fabricplans: Fabricplan[];



ngOnInit() {

this.productservice.getProducts().subscribe(products => {
    this.products = products;
    console.log("Produtos", this.products);

    this.products.forEach( element => {
      console.log(element.fabricPlanId);
      this.fabricplanservice.getfabricplan(element.fabricPlanId).subscribe(
        (response) => {
          this.fabricplan = response;
          this.plan_desc.push(this.fabricplan['description']);
        },
        (error) => {
          if(error.status === 404){
            this.plan_desc.push('No fabric plan');
          }
        });
      });
  });

 console.log("Planos de fabrico", this.plan_desc);
}
制作计划

{
active: true,
description: " descrição do prod 1",
fabricPlanId: 1,
id: 1,
name: "Produto 1",
price: 1
}
{
dateStart: "2019-10-30T00:00:00"
description: "Descrição do plano 1"
id: 1
operationsIds: [1, 2]
}

根据您的评论,
计划描述
是随机顺序。原因是

this.products.forEach( element => { 
      this.fabricplanservice.getfabricplan(element.fabricPlanId).subscribe(
    ...
您无法控制每个请求所需的时间,因此有些请求会更快返回,有些则会更晚返回,并且无论何时返回,它们都会被添加到列表-->顺序是随机的

但是,为每个项目发出请求,然后使用rxjs获得列表顺序是非常容易的

(我在这里写了他的代码)

进口:

import {forkJoin, throwError} from 'rxjs';
import {map, catchError} from 'rxjs/operators';

问题是什么?我面临的问题是数组中的值与产品对应的
fabricPlanId
不匹配。哪个数组?plan_desc?我从“rxjs”添加了
import{forkJoin}但仍然出现一些错误,例如,找不到名称映射、catcherror和thorwerror。缺少什么导入?我已经在你发布的链接中找到了它们,谢谢,还有一件事forkjoin中的
plans\u desc$
出现了以下错误`类型为'void'的参数不能分配给类型为'observeInput'的参数。`啊,是的,它不是
foreach
,而是标准的JS映射,因为您正在将通道映射到另一个阵列
// create a request for each product (dont fire it yet)
const plans_desc$ = this.products.map( element => 
  this.fabricplanservice.getfabricplan(element.fabricPlanId).pipe(
    // map it to the value you want
    map((response) => {
      this.fabricplan = response;
      return this.fabricplan['description'];
    }),
    // replace 404 responses with the  'No fabric plan'
    // if not 404 throw the error again
    catchError((error) => {
      if(error.status === 404){
        return 'No fabric plan';
      } else {
        throwError(error);
      }
    }));
  });

// now make the actuall requests. Forkjoin will return, when all requests are completed.
// the order will be the be how the requests where added, not when the completed
forkJoin(plans_desc$).subscribe((plans_desc) => this.plan_desc = plans_desc);
import {forkJoin, throwError} from 'rxjs';
import {map, catchError} from 'rxjs/operators';