1
votes

Trying to communicate with 2 components I thought I'd be able to make a http call, then maybe mergeMap or switchMap to a subject?

Something like

import {Subject} from 'rxjs/Subject';

 constructor(private _http: HttpClient) {
    this.populateList = new Subject<Blog[]>();
 }

getBlogs(){
  return this._http.get(this.blogsURL+'blogs')
       .map((result: Response ) => {
       this.blogs = result['blogs'];
    return this.blogs;
  }).switchMap((blogs)=>this.populateList.next(blogs))
}

But I get:

You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array

I'm getting errors just trying to subscribe to the subject:

 this.blogsService.populateList()
      .subscribe((res)=>{
      console.log(res)
  })

this.blogsService.populateList is not a function

What I'm looking for is a way to update views after http calls

1

1 Answers

1
votes

You need to subscribe like this without (). Cause its not a function. surprise

 this.blogsService.populateList.subscribe()

and rewrite first function like this cause you dont need switch map you just need to do is side effect to populate list.

getBlogs(){
  return this._http.get(this.blogsURL+'blogs')
       .map((result: Response ) => {
       this.blogs = result['blogs'];
    return this.blogs;
  }).do((blogs)=>this.populateList.next(blogs))
}