I'm building an app similar to Instagram, I'm currently stuck trying to get the likes of a post and I was wondering if someone could help me. I use a Laravel API and it works fine. The problem is that I don't know how to add the likes to my items array.
Here is my code:
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {PostsDataProvid`enter code here`er} from '../../providers/posts-data/posts-data';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [PostsDataProvider]
})
export class HomePage {
public items =[];
constructor(public navCtrl: NavController, public getPostsService: PostsDataProvider) {
this.getPostsService.getAllPosts().subscribe((data) => {
for(var i=0; i < data.length; i++){
this.items.push(data[i]);
this.getPostsService.getPostLikes(data[i].id).subscribe((countLikes) => {
this.items[i].likes = countLikes.length;
// if I change countLikes.length with 2 for example it works perfect ( outside this function but within the for loop)
});
}
});
}
}