asynchronous - Angular2 - updating variable after function (promise?) -


i havea problem. in registration want view error message if user exists in database. created "serveranswer" variable handle error answer server.

my register.service.ts content

@injectable() export class registerservice {   constructor(private http: http) {}    public serveranswer: object;    getanswer() {     return this.serveranswer;   }    adduser(user: user) {     var headers = new headers();     headers.append('content-type', 'application/json');      this.http.post('/users/register', json.stringify(user), {headers: headers})     .subscribe(       response => {         if(response.json().success) {           window.location.href = "/";         }         if(response.json().error) {           this.serveranswer = response.json();         }       }     )   } } 

as can see, @ start "serveranswer" variable empty. "response.json().error" handle error text. okay, it's time show register.component.ts content.

export class registercomponent {   constructor(private http: http, private registerservice: registerservice) {}    serveranswer: object;    onsubmit(username: string, userpassword: string, useremail: string) {     this.registerservice.adduser(new user(username, userpassword, useremail))     this.serveranswer = this.registerservice.getanswer();   } } 

the problem "systemanswer" registerservice has been updated before function adduser done. result if user exists in database, first time "systemanswer" empty , second time handling response.json().error text.

this place:

this.registerservice.adduser(new user(username, userpassword, useremail)) this.serveranswer = this.registerservice.getanswer(); 

what should update serveranswer variable when adduser function done?

thank help!

it better if return observable object service:

@injectable() export class registerservice {   constructor(private http: http) {}    adduser(user: user) {     var headers = new headers();     headers.append('content-type', 'application/json');      return this.http.post('/users/register', json.stringify(user), {headers: headers})         .map(response => response.json());   } } 

and use in controller:

export class registercomponent {     constructor(private http: http, private registerservice: registerservice) {}      serveranswer: object;      onsubmit(username: string, userpassword: string, useremail: string) {         this.registerservice.adduser(new user(username, userpassword, useremail))             .subscribe(                 response => {                     if (response.success) {                         window.location.href = "/";                     } else if (response.error) {                         this.serveranswer = response;                     }                 }             );     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -