我正在开发一个应用程序,其中我有一个回收垃圾桶系统.例如,当它太满时,我希望能够加载10到10个项目,但我有一个映射错误.以下是我收到的错误,我不知道如何解决它:

TypeError:无法读取未定义的属性(正在读取‘map’)

如果您抽出时间来回答的话,我会提前感谢您.

服务器端: 这是我的控制器:

    #[Route(
        name: 'get_all_fiche_deleted_for_user', path: '/deleted', allowed_methods: [HttpMethod::GET], middleware: [AuthMiddleware::class]
    )]
    public function getAllFicheDeletedForUser(EntityManager $entity_manager, Request $request, int|Utilisateur $utilisateur = null): array
    {
        $queryParam = $request->getQueryParams();
    
        $limit = isset($queryParam['limit']) ? $queryParam['limit'] : 2;
        $offset = isset($queryParam['offset']) ? $queryParam['offset'] : 0;

        $utilisateurRequest = $request->getAttribute('utilisateur');

        if (is_int($utilisateur)) {
            $utilisateur = $entity_manager->getRepository(Utilisateur::class)->find($utilisateur);
        }

        $result = $entity_manager->getRepository(Panier::class)->getAllFichesdeleted($limit, $offset, $utilisateurRequest);
    
        return $result; 
    }

客户端: 这是我的服务:

  getAllPanierFichesDeletedForUser(limit: number, offset: number): Observable<Panier[]> {
    const params = new HttpParams()
    .set('limit', limit.toString())
    .set('offset', offset.toString());
    
    return this.httpClient.get(
      this.getDomainApi() + '/v1/panier/deleted', this.getHttpOptions()
    ).pipe(
      map((response: any) => {
        return response.data.map((item: any) => deserialize(Panier, item));
      })
    );
  }

There is the result of a console.log(response):

最后是我的组件:

  ngOnInit() {
    this.loadDeletedFiches();
  }

  loadDeletedFiches() {
    this.panierService.getAllPanierFichesDeletedForUser(10, 0).subscribe((paniers: Panier[]) => {
      console.log('Données reçues :', paniers);
      this.paniers = paniers;
      this.isLoading = false;
    });
  }

推荐答案

请将代码更改为,numeros是响应中包含数组的属性,这是我们需要的主要数据!

map是一个属于数组类型的Java脚本函数,它会根据你提供的mapFunction作为输入(item: any) => deserialize(Panier, item)将数组转换成一个新的数组!

getAllPanierFichesDeletedForUser(limit: number, offset: number): Observable<Panier[]> {
    const params = new HttpParams()
    .set('limit', limit.toString())
    .set('offset', offset.toString());
    
    return this.httpClient.get(
      this.getDomainApi() + '/v1/panier/deleted', this.getHttpOptions()
    ).pipe(
      map((response: any) => {
        return response.numeros.map((item: any) => deserialize(Panier, item));
      })
    );
  }

Typescript相关问答推荐

如何从具有给定键列表的对象类型的联合中构造类型

在TypeScript中使用泛型的灵活返回值类型

找不到带名称的管道''

SortKey类型不起作用,尽管它使用的单个类型工作正常<>'

如何推断哪个特定密钥与泛型匹配?

如何在TypeScrip中从字符串联合类型中省略%1值

返回具有递归属性的泛型类型的泛型函数

为什么Typescript不能推断类型?

在列表的指令中使用intersectionObservable隐藏按钮

避免混淆两种不同的ID类型

为什么TypeScrip不能解析对象析构中的赋值?

是否将Angular *ngFor和*ngIf迁移到新的v17语法?

TaskListProps[]类型不可分配给TaskListProps类型

递归生成常量树形 struct 的键控类型

如何使用angular15取消选中位于其他组件中的复选框

如何调整项目数组,但允许权重影响顺序

如何从联合类型推断函数参数?

TS 排除带点符号的键

递归类型名称

从平面配置推断嵌套递归类型