2

The code listed in (A) never sends its HTTP request when test() is called whereas the almost identical code in (B) does work. Any idea what the problem is? I know whether the request is sent by watching the server logs. Also, if I make the request in (A) manually by pasting it into the browser, I get the expected response. I'm stumped!

(A)

import { Constants } from '../toplevel/constants'

import { Injectable }     from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable} from 'rxjs/Rx';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


@Injectable()
export class SigninService {

  constructor (private http: Http, private constants : Constants) {

  }

  getToken(username: string, password: string) : Observable<string>{

    var url = `${this.constants.apiRoot}/users/${username}?${password}`

    console.log(`Calling api server with url ${url}`)

    return this.http.get(url)
      .map((res:Response) => JSON.stringify(res.json()))
      .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

  }

 test() {

   this.getToken('jc', 'yadayada')
 }
}

(B: excerpt)

getDocument(id: string) : Observable<Document>{
  return this.http.get(`${this.apiUrl}/documents/${id}`)
  .map((res:Response) => new Document(res.json()['document']))
  .catch((error:any) => Observable.throw(error.json().error || 'Server error'));

  }
3
  • Also, I do see the messages posted by console.log Commented Dec 2, 2016 at 15:07
  • 1
    From the (A) code, you have an error code or something ? Could you try to add .subscribe() next to your catch to see ? Commented Dec 2, 2016 at 15:13
  • Have you use debugger; in your function to see what is happening step by step? Commented Dec 2, 2016 at 15:14

1 Answer 1

2

Your sequence is cold. You need to change your test to:

   this.getToken('jc', 'yadayada').subscribe()

to make it active and send the request

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.