【问题标题】:Correctly declare Typescript type inside Observable map function在 Observable 映射函数中正确声明 Typescript 类型
【发布时间】:2018-06-26 23:10:09
【问题描述】:

我在 Angular 服务中有这个 fetch 函数。一切正常,但 TypeScript 抱怨 “类型 'Object' 上不存在属性 'statuses'。”

我想我需要为数据对象创建一个接口,但是当我尝试这样做时,我不确定在哪里声明它。我也尝试声明 HttpResponse 但这需要一个参数。任何线索都会很棒!

export class Status {
  constructor(
    public workflowId: string,
    public spawnTime: number,
    public spawnValue: string,
    public workflowName: string
  ) {
  }
}

fetch(command, params?) {
return this.http.post(`http://localhost:4000/${command}`, params)
  .pipe(
    map((data): Status[] => {
      const statuses = [];
      for (const status of Object.keys(data.statuses)) {
        const newStatus = new Status(
          status,
          data.statuses[status]['spawn-time'],
          data.statuses[status]['spawn-value'],
          data.statuses[status]['workflow-name']
        );
        statuses.push(newStatus);
      }
      return statuses;
    })
  )
  .pipe(
    catchError(this.handleError)
  );
}

【问题讨论】:

  • Status 对象(属性)中有什么?也是data 返回的类型Status[]?很确定它是一个Response 对象。
  • 我在帖子中添加了更多信息
  • 你能从你的数据中提供一些示例输出吗?我创建了 stackblitz 试图复制它。 stackblitz.com/edit/angular-p2zfrw
  • data['statuses'][status]['spawn-time']

标签: angular typescript


【解决方案1】:

好的,我在其他帖子的帮助下找到了答案。我必须为data 创建一个接口,因为来自http 请求的响应只是一个json 对象。抱歉,我花了一段时间才意识到发生了什么。这是不引发 TS 错误或警告的完整工作解决方案:

export interface Data {
  statuses: object;
}

@Injectable({
  providedIn: 'root'
})
export class EpisodeApiService {

  constructor(private http: HttpClient) {
  }

  fetch(command, params?) {
    return this.http.post(`http://localhost:4000/${command}`, params)
      .pipe(
        map((data: Data) => {
          const statuses: Status[] = [];
          for (const status of Object.keys(data.statuses)) {
            const newStatus = new Status(
              status,
              data.statuses[status]['spawn-time'],
              data.statuses[status]['spawn-value'],
              data.statuses[status]['workflow-name']
            );
            statuses.push(newStatus);
          }
          return statuses;
        })
      )
      .pipe(
        catchError(EpisodeApiService.handleError)
      );
  }
}

【讨论】:

    【解决方案2】:

    这是一个如何解析json信息的例子。 在此示例中,您需要在 first map 的内部进行 map inside,它将作为迭代器工作,然后返回解析为接口 @987654323 的对象@

     export interface Status {
           status1: string;
           status2: string;
           status3: string;
        }   
    
    
        fetch(command, params?) {
          return this.http.post(`http://localhost:4000/${command}`, params)
            .pipe(
               map((res: any) => {
                  return res.map(res =>{
                       return <Status> {
                          status1: res.stausOne,
                          status2: res.stausTwo,
                          status3: res.stausThree,  
                       }
                   });                   
               }))
            .pipe(
                catchError(this.handleError)
             );
    

    【讨论】:

    • 当我尝试这个时,我得到一个错误,因为map 不存在于类型&lt;Observable&gt; 上。 (我在 Angular 6 上,顺便说一句)
    • 你的意思是不能在post句后加map??\
    • 正确。 this.http.post(...).map(res... 导致错误 Property 'map' does not exist on type 'Observable&lt;Object&gt;'.
    • ok 看看完整的例子我添加了方法类型和返回语句
    • 谢谢,但它仍然不喜欢map 功能。我在其他地方读到过,使用 Angular6 我们需要将map 包装在pipe 中。当我包裹在pipe 中时,它需要getEvents(): Observable&lt;Object&gt;,但随后res.map 不起作用,因为对象上没有map 方法。
    猜你喜欢
    • 2021-10-19
    • 2021-09-07
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多