Within my Angular2 app, I have created an Observable in a Service called ContentService. Here is a simplified version of the code:
@Injectable()
export class ContentService {
constructor(private http:Http, private apiService:ApiService) {
this.content = this.http.get('src/i18n/en.json')
.map((res:Response) => {
let json: {} = res.json();
return mapData(json);
})
mapData() {
// function stuff to format data
}
Now, I need to combine the results from the apiService with the results from this.content
. The apiService returns a JSON object with a similar structure as this.content
, but it comes from a third party API. I tried using flatMap to achieve this but encountered some syntax errors. My attempt looked like this:
this.content = this.http.get('src/i18n/' + this.userLang + '.json')
.map((res:Response) => {
let json: {} = res.json();
return mapData(json);
})
.flatMap(() => {
apiService.getAllCurrentListings().subscribe(response => {
return mapData(response);
})
});
This approach led to an error, indicating that there was a mistake in my syntax. When I call my API service outside the flatMap function, it works fine. So, clearly, I am missing something in the way I'm trying to add the apiService data to the original this.content
results. Can someone help me understand how to do this correctly?
Thank you for your assistance.