首页 > 解决方案 > TypeScript 地图不是函数

问题描述

我收到错误“data.map 不是函数”。我正在映射返回 JSON 数据的 REST API 请求,但问题似乎与数据结构有关。我认为返回的数据是 data.data 并且我没有正确映射它。任何建议都会有所帮助。

这是数据结构。 JSON数据结构

这是我的服务代码。

  getAlladvisors$ = this.http.get<IAdvisor[]>("http://localhost:8055/items/advisor")
    .pipe(
      map((data: IAdvisor[]) =>
    data.map(
      a =>
      ({
        name: a.name,
        id: a.id,
        page: a.page,
        multilang: a.multilang
      })
    )
  ),

AdvisorsandQuestions$ = this.http.get<IAdvisorsAndQuestions[]>("http://localhost:8055/items/question")
  .pipe(
    tap(data => console.log("Questions IDs", data))
  );
    
getAdvisorsWithId$ = combineLatest([
      this.getAlladvisors$,
      this.AdvisorsandQuestions$
    ]).pipe(
      map(([product, categories]) =>
        product.map(product => ({
          ...product,
          questionId: categories.find(c => product.id === c.id).questions_Id,
        }) as IAdvisor)
      ),
    );

这是界面

export interface IAdvisor {
  id: number,
  name: string,
  page: string,
  multilang: boolean  
}

标签: angulartypescriptapirxjs

解决方案


我不确定我的答案,但对于数据:<IAdvisor []> 它可能只是数据。

  getAlladvisors$ = this.http.get<IAdvisor[]>
       ("http://localhost:8055/items/advisor")
       .pipe(
            map((data: <IAdvisor[]> => {
              if (data) {
                console.log(data);
              }
              return data;
            }),
            catchError((error) => {
                 console.log(error)
           }
         })

推荐阅读