2

I'm struggling with this, I'm making a request and I'm receiving an object, I can't iterate it, so i don't know How I can accomplish, this is my code:

I have a Interface called CountryCode

my services file looks like this:

```import { OperatorCode } from './../../models/catalogs/OperatorCode';
import { CountryCode } from '../../models/catalogs/CountryCode';
import { environment } from './../../../environments/environment';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class DatosInicialesService {
  baseUrl = environment.baseUrl;

  constructor(private http: HttpClient) {}
  getCountryCode(): Observable<CountryCode[]> {
    return this.http.get<CountryCode[]>(
      `${this.baseUrl}/api/Catalog/CountryCode`
    );
  }```

and my component file looks like this:

    import { CountryCode } from './../../models/catalogs/CountryCode';
    import { DatosInicialesService } from './../../services/actualizacion-de-datos/datos-iniciales.service';
    import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';
    
    @Component({
      selector: 'app-datos-iniciales',
      templateUrl: './datos-iniciales.component.html',
      styleUrls: ['./datos-iniciales.component.scss'],
    })
    export class DatosInicialesComponent implements OnInit {
      countryCode = [];
      operatorCode = [];
      constructor(
        private router: Router,
        private datosInicialesService: DatosInicialesService
      ) {}
    
      onSubmit(event: any) {
        event.preventDefault();
        this.router.navigate(['actualizacion-de-datos/mas-sobre-ti']);
      }
    
      ngOnInit(): void {
        this.datosInicialesService.getCountryCode().subscribe((data) => {
          this.countryCode = data
          console.log(data);
        });
    
    }```

and this is what it shows in the console:

    {Data: Array(252), Status: {…}, Info: {…}}
Data: (252) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, 
...

Info: {Datetime: "2021-02-24T20:23:07.3906084+00:00", AcceptedUser: true}
Status: {Code: 200, Message: "Ok"}

How can I loop through that response and get the name and the description of that Data response, I'm pretty new to web development so that's why this question might be mocked. I have tried in different ways to loop that object but I can't find the way to access to those properties I need.

Thanks in advanced if somene can help me with that
2
  • Can you show your CountryCode interface ? Commented Feb 24, 2021 at 20:43
  • Yes sure. Data: { idNationality: string; code: string; name: string; description: string; }[]; Status: Status; Info: Info; }` Commented Feb 24, 2021 at 20:56

2 Answers 2

2
ngOnInit(): void {
        this.datosInicialesService.getCountryCode().subscribe((data) => {
          this.countryCode = data
          data[0].Data.forEach( (element) => {
            console.log(element.name);
            console.log(element.description);
            });
        });
Sign up to request clarification or add additional context in comments.

2 Comments

It shows me an error. Property 'Data' does not exist on type 'CountryCode[]
check edited answer if it solves your problem, you have something malformed there for sure
0

This is my CountryCode interface : import { Status } from '../status.base'; import { Info } from '../info.base';

  Data: {
    idNationality: string;
    code: string;
    name: string;
    description: string;
  }[];
  Status: Status;
  Info: Info;
}`

2 Comments

it throws error because it checks the type of variable and your interface doesn't has data property. It will compile because eventually it will compile. If you want to avoid the error, just add data to your interface
what do you mean? I'm working with that structure of the interface because it already was used in othjer projects but the devloper who has worked with that before it not working here anymore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.