【问题标题】:I think my Problem is that the API delivers an Object instead of an array. So i need to Modify the Object to be an Array?我认为我的问题是 API 提供了一个对象而不是一个数组。所以我需要将对象修改为数组?
【发布时间】:2021-04-02 22:02:11
【问题描述】:

我遇到了这个错误 ERROR 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。

我正在尝试从 API 获取数据,但它向我显示了此错误,因为我仍在学习 Angular,请指导我如何解决此类错误。

  1. 元数据服务.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Pincodes } from 'src/app/Model/Pincodes';
import { baseURL } from 'src/environments/environment';

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

  constructor(private http:HttpClient) { }

  public GetPincodes() : Observable<any> {
    console.log("Calling GetPincodes API")
    return this.http.get("/Metadata/api/metadata/getpincodes") // My API Link
  }

}

  1. API 包含以下数据
{
"program": null,
"version": null,
"dateTime": null,
"success": true,
"errorCode": null,
"errorMessage": null,
"message": "Pincodes read Successfully",
"data": [
{
"pincode": 400708,
"village": "Airoli",
"taluka": "Thane",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 416007,
"village": "Kaamba",
"taluka": "Kalyan",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421102,
"village": "Atali",
"taluka": "Kalyan",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421501,
"village": "Ambarnath",
"taluka": "Ambarnath",
"district": "Thane",
"state": "Maharashtra"
},
{
"pincode": 421503,
"village": "Ambeshiv",
"taluka": "Ambarnath",
"district": "Thane",
"state": "Maharashtra"
}
]
}

我对数据数组感兴趣

  1. Pincode.ts

export class Pincodes {
    pincode : number
    village : string
    taluka : string
    district : string
    state : string
}
  1. register-page.component.ts
import { Component, OnInit } from '@angular/core';
import { data } from 'jquery';
import { Pincodes } from 'src/app/Model/Pincodes';
import { MetadataServiceService } from 'src/app/shared/services/metadata-service.service';
import { Albums } from "src/app/Model/albums";
import { Observable, of } from 'rxjs';
import {MatTableDataSource, MatTableModule} from '@angular/material/table';
import { report } from 'process';


@Component({
  selector: 'app-register-page',
  templateUrl: './register-page.component.html',
  styleUrls: ['./register-page.component.css']
})

export class RegisterPageComponent implements OnInit {


 observeData : Pincodes[]  
  modifiedText: any;
  PinSelected : any = {}

 
 


  constructor(private _metadataService: MetadataServiceService) {  }

  ngOnInit(){
    
   this._metadataService.GetPincodes()
   .subscribe(data => {
      this.observeData = data;
    });
  }
onPincodeSelected(val : Pincodes){
    console.log("value" + val);
    this.customFunction(val)
  }
  customFunction(val : Pincodes){
    this.modifiedText = "The Selected Pin was " + val.pincode + " and selected District is " + val.village
      console.log("modifiedText" + this.modifiedText);
      console.log("Pincode Selected" + this.PinSelected);
      console.log("observeData Selected" + this.observeData);
      
      
  }
  
}

  1. register-page.component.html
<select
        [(ngModel)]="PinSelected"
        (ngModelChange)="onPincodeSelected($event)"
      >
        <option *ngFor="let pin of observeData" [ngValue]="pin">
          {{ pin.pincode }}
        </option>
      </select>
      <div>
        <p>{{ modifiedText }}</p>
      </div>

错误错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。

它显示这个错误。

【问题讨论】:

    标签: json typescript api object angular9


    【解决方案1】:

    您需要迭代的数组在您的 api 响应对象中,所以在

    this._metadataService.GetPincodes()
       .subscribe(data => {
          this.observeData = data;
        });
    

    this.observeData = data 更改为this.observeData = data.data

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 2019-12-21
      • 2021-11-14
      • 2023-01-25
      • 2022-01-22
      • 1970-01-01
      • 2015-05-30
      • 2021-06-07
      相关资源
      最近更新 更多