简体   繁体   English

如何将 API Object 转换为数组

[英]How to convert API Object to Array

I stuck with this error ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'.我坚持这个错误错误错误:找不到不同的支持 object '[object Object]' 类型 'object'。 NgFor only supports binding to Iterables such as Arrays. NgFor 仅支持绑定到 Iterables,例如 Arrays。

I am trying to fetch data from API but it's showing me this error, as I am still learning angular please guide me on how to tackle this kind of errors.我正在尝试从 API 获取数据,但它向我显示此错误,因为我仍在学习 angular 请指导我如何解决此类错误。

  1. metadata-service.service.ts元数据服务.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 consist of the following data 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"
    }
  ]
}

I am interested in the data array我对数据数组感兴趣

  1. Pincode.ts Pincode.ts
export class Pincodes {
    pincode : number
    village : string
    taluka : string
    district : string
    state : string
}
  1. register-page.component.ts注册页面.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注册页面.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>

Error: Cannot find a differ supporting object '[object Object]' of type 'object'.错误:找不到支持 object 类型为“object”的“[object Object]”。

Try this:试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM