Angular 财产';地图';不存在于类型';对象';

Angular 财产';地图';不存在于类型';对象';,angular,typescript,Angular,Typescript,我最近进入Angular和TypeScript,试图学习一些东西,并找到了一个我可以尝试的特定项目(如果有人想看这个项目,我可以在链接中编辑)(尝试从pokemon api读取json数据) 此行中的类型“object”上不存在属性“map”。 .then(items=>items.map((item,idx)=> 这是完整的代码 import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/co

我最近进入Angular和TypeScript,试图学习一些东西,并找到了一个我可以尝试的特定项目(如果有人想看这个项目,我可以在链接中编辑)(尝试从pokemon api读取json数据)

此行中的类型“object”上不存在属性“map”。
.then(items=>items.map((item,idx)=>

这是完整的代码

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class PokedexService {
  private baseUrl: string = 'https://pokeapi.co/api/v2/pokemon/';
  private baseSpriteUrl: string = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/';

  constructor(private http: HttpClient) { }

  getPokemon(offset: number, limit: number) {
    return this.http.get(`${this.baseUrl}?offset=${offset}&limit=${limit}`)
      .toPromise()

      //.then(response => response.json().results)
      //.map((results = > response.json()))

      .then(items => items.map((item, idx) => {
        const id: number = idx + offset + 1;
        return {
          name: item.name,
          sprite: `${this.baseSpriteUrl}${id}.png`,
          id
        };
      }));
  }
}
当我试图运行这个项目时,我首先得到了这个脚本的json部分的一个错误(json不存在于对象上),然后我了解到,在新的更新中,您不需要这些行,所以我将它们注释掉(不知道这是对还是错),然后我得到了我现在正在询问的错误

我希望我没有做错任何事,并等待任何指导。
同样,我在这方面是新手,但知道编码。

如果您声明返回值(即Items[]),您将能够运行.map 我通常在API服务和组件之间进行分离,因此看起来是这样的:

api.service.ts

public GetPokemon(): Observable<Items[]> {
        const url = this.url_to_backend_method;
        return this.http.get<Items[]>(url)
            .pipe(catchError(this.handleError));
    }

希望这对你有所帮助。错误告诉了你你需要知道的一切。你的
是一个对象,而不是数组。你也不应该使用
来渲染
,而只是
订阅
。错误准确地解释了错误的原因。你试过控制台.log(项)吗?它们看起来像什么?你只能在数组中使用“.map”。使用
项。结果。map(…)
@ritaj我根据你的评论更改了我的代码,这花了我一段时间,但效果很好。现在我可以运行(ng服务)该应用程序。我只需要检查一下我是否获得了数据集。谢谢。
private getPokemon() {
        this.api.GetPokemon().subscribe(
            items => items.map((item, idx) => {
                ** run your logic here **};
            }, error => {
                ** deal with error here **
            });
    }