0

Probably just a silly mistake somewhere but i cant seem to pinpoint the cause. The function is being called (verified by the console log) but for some reason neither the error log or the success log print out afterwards. The back end is not receiving the post request, so why is the error log not showing anything?

getBooks(): Observable <FormData | null> {
  let request = new FormData();
  request.append('action', "GetBooks");

  console.log("Function has been called");

  return this.http.post('http://localhost/Projects/Website/PHP/index.php', request).pipe(
    map((returned) => {
      console.log("Here I parse 'returned'");

      let books = new FormData();

      return books;
  }, (error) => {
    console.log('Error! ', error);
    return null;
  }));
}
4
  • 3
    Call the method with getBooks().subscribe() Commented May 17, 2019 at 21:04
  • @MolikMiah why? Commented May 17, 2019 at 21:04
  • 1
    Sorry for lack of information im typing on a mobile. When you set up a method like this you are returning an observable. Think of it as provisioning a http request. To actually make the call and get the data returned you must subscribe to it. Please have a look at angular docs for http and they will provide a better explanation Commented May 17, 2019 at 21:14
  • 1
    This page will explain it angular.io/guide/http Commented May 17, 2019 at 21:15

1 Answer 1

3

you need to call .subscribe() so your method will be used.

since your work with an observable, subscribe is the function that "listen" to any data coming from the observable, which actually execute the code.

     var res=null;
     this.http.post('http://localhost/Projects/Website/PHP/index.php', 
     request).subscribe(data => {
          res=data;
          },
          error => {
           ...
          },
          () => {
          ...
          }
          return res;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.