Ionic ERROR [object object]

0

How can I solve the following error?

I leave the code of my provider:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class WordpressProvider {

baseUrl:string ="http://mipagina.com/wp-json/wp/v2/posts/9159";

  constructor(public http: HttpClient) {
    console.log('Hello WordpressProvider Provider');
  }

  recibirEventos(){
    return this.http.get(this.baseUrl);
  }

}

The one in my home.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { WordpressProvider } from './../../providers/wordpress/wordpress';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

datos
  constructor(public navCtrl: NavController,
              public http: HttpClient,
              public wordpress: WordpressProvider) {


    this.wordpress.recibirEventos().subscribe(data=>{
      this.datos = data;
      });

  }
}

and the home.html

<ion-list>
<ion-item *ngFor="let item of datos">
{{item.id}}
</ion-item>
</ion-list>
    
asked by Pablo Reyes 04.11.2018 в 19:05
source

1 answer

0

It would be necessary to see what type of data is arriving through the request get, that according to the error is not an array or iterable object. This can be solved by making a console.log()

this.wordpress.recibirEventos().subscribe(data=>{
  this.datos = data;
  console.log(data);
  });

or using a more specific program such as the Postman that allows you to manage all types of queries and their response.

In order to avoid errors it is advisable to manage the type of variable that you are going to assign, in this case, to the variable this.data that could calmly be declared

export class HomePage {

public datos: any[];
...

and if you can be more specific in the type of data much better (instead of any) besides that it will be published ( public ) because we will use it in the HTML to populate the list. Now if we want to make the object iterable from your service / provider you can do the following

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class WordpressProvider {

baseUrl:string ="http://mipagina.com/wp-json/wp/v2/posts/9159";

  constructor(public http: HttpClient) {
    console.log('Hello WordpressProvider Provider');
  }

  recibirEventos(){
    return this.http.get(this.baseUrl).map(res => res.json());
  }

}

I am relying on the following problems reported in StackOverflow in English and the Ionic forum and I hope it's the solution to your problem

    
answered by 05.11.2018 в 06:53