I am starting with Ionic and Angular and I am facing an issue with variable type. I am querying an API that ouput an array of elements.
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {PostService } from '../_providers/post-service';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers:[PostService]
})
export class HomePage {
public posts: any;
private start:number=0;
constructor(public navCtrl: NavController, public postService: PostService) {
this.getPost();
}
getPost(){
return new Promise(
resolve => {
this.postService.load(this.start)
.then(data => {
console.log(data);
for(let post of data) {
this.posts.push(post);
}
resolve(true);
});
});
}
doInfinite(infiniteScroll:any) {
console.log(this.start);
this.start+=10;
this.getPost().then(()=>{
infiniteScroll.complete();
});
}
likePost(post){
post.likes++;
}
}
Line 23, I have this issue: issue
And the console.log returns: console.log
Finally, the PostService class looks like this:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class PostService {
perpage:number = 10;
constructor(public http: Http) {}
load(start:number=0) {
return new Promise(resolve => {
this.http.get('http://api.voyonsvoir.fr/?[limit]='+this.perpage+'&filter[skip]='+start)
.map(res => res.json())
.subscribe(data => {
resolve(data.posts);
});
});
}
}
Thanks for your help.
getPost(){ return new Promise( resolve => { this.postService.load(this.start) .then(data => { for(let post in data) { this.posts.push(data[post]); } resolve(true); }); }); }