1

I have the following in my service:

 return this.http.get(this.wakeUrl)
                        // ...and calling .json() on the response to return data
                         .map((res:Response) => {res.json()
                            console.log("Hello") 
                            })
                         //...errors if any
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

I can see in the network tab in chrome that the response came back successfully with a 200, but how do I log it in the console? In my component, I want to subscribe to that, but am not getting any output either...

this.myService.makeHttpCall().subscribe(
  data => {
    console.log(data)
  },
  err => {
    console.log(err);
  });

How do I log: 1- As soon as the data comes back in my service? 2- Within the component that I injected the service into?

Rolando
  • 58,640
  • 98
  • 266
  • 407

2 Answers2

1

If you want to have your console.log in the mapping, you need to change the order of your console and then actually return the response:

.map((res:Response) => { console.log("Hello"); return res.json()})

without the console log, it should work fine with:

.map((res:Response) => { return res.json()})

or simply

.map(res => res.json())

And when you want to console log the data in your component, you just do it inside the callback (subscribe), like you have. Somewhat related: Since we are dealing with asynchronous events here, this could be a useful read for future reference, which also explains the position of the console log in the component: How do I return the response from an Observable/http/async call in angular2?

Community
  • 1
  • 1
AT82
  • 71,416
  • 24
  • 140
  • 167
0

(1) In the HTTP protocol, the raw data can be found two lines("\r\n\r\n") under the headers, if that is what you are willing to display. Otherwise, if you just want to make sure you're getting a 200 response, just make the right REGEX and make an if statement.

(2) I don't understand your question. If you injected the service directly to your server, that means you are the server therefore no need fore it.

If you do something that makes you the client you are not injecting the service, you just provide a client service. If it's the other case then you can just an extra 200 yourself. That is something that could potentially change between services, I guess the best way would be just to sniff your network while using the service and finding out yourself.

Gal Niv
  • 68
  • 3