0

I'm trying to get into ionic. Now I have a problem: I have a loginpage + controler (login.ts), provider for the autentification (auth.ts) and provider for the http request (http-request.ts). Here is the code:

login.ts

login(user_name, user_password){
    this.auth.login(user_name, user_password);
}

auth.ts

login(user_name, user_password)
{

  let fields_db   = 'user_id,user_displayname,user_firstname,user_lastname,user_password,user_email,user_gender,user_city,user_dateofbirth,user_dateofregistry,user_dateoflastlogin,user_image,user_status';
  let filters_db  = '(user_displayname='+ "Jon" +') and (user_password='+ "123456" +')';
  let tablename   = 'user';

  return this.httpRequest.get(fields_db, filters_db, tablename).subscribe(
  result => {
    this.userdataprovider.setLoggedUser(result);
    console.log(result);
    return result;
  }, 
  error => {
     console.log('Error bei der Authentifizierung ' + error);
     return error;
  }
);

}

http-request.ts

request = (req) => {

  let baseUrl = REST_BASE_URL,
      requestOptions = new RequestOptions({
      method: req.method,
      url: baseUrl+req.url,
      headers: req.header ? req.header : this.getHeader(),
      body: req.params
  });

  return this.http.request(new Request(requestOptions)).map((res:Response) => res.json());
}

get(fields_db, filters_db, tablename){
  let params = {
    filter: filters_db,
    fields: fields_db
  };

  return this.request({method: 'GET', url: (TABLE_URL_PART + tablename), params: params});
}

Http provides a result, then i subscribe in out. But every time in login I get undefine. That I can see over console: console.log(this.auth.login(user_name, user_password));

How I have to implement login to subscribe auth function or data, that I get from http? What is the best solution.

Thanks a lot :)

Andy
  • 1
  • You cannot return anything from `subscribe`. You need to return from `map` and subscribe in component :) – AT82 Aug 05 '17 at 13:38
  • Possible duplicate of [angular 2 how to return data from subscribe](https://stackoverflow.com/questions/39295854/angular-2-how-to-return-data-from-subscribe) – AT82 Aug 05 '17 at 13:39
  • What do you mean by return from map and subscribe in component? I subscribe a function in auth.ts. My stucture is login()->auth()->http-request()-http(). The last http is asynchronous and provide a return aftter same time. Now I need to make my own function (login, auth and http-request) also asynchron, but how? I need to add a subascriber to each function ? – Andy Aug 06 '17 at 09:57
  • You are trying to return from subscribe: `return result;` from: `return this.httpRequest.get(fields_db, filters_db, tablename).subscribe(` subscribe won't return :) – AT82 Aug 06 '17 at 10:00
  • you can use `map` instead and then subscribe `this.auth.login(user_name, user_password).subscribe(result => .....)` – AT82 Aug 06 '17 at 10:03
  • If you try and subscribe in service and wouldn't even try and return anything, you don't know how long the request would take. So when you subscribe in component, you know for sure that the code has been executed and after that do whatever you need to do. – AT82 Aug 06 '17 at 10:10
  • 1
    Thanks a lot :) It's works. – Andy Aug 06 '17 at 11:37
  • Great! Glad to hear it worked out :) – AT82 Aug 06 '17 at 15:08

0 Answers0