I've added the code below. Basically I want to show error messages to different scenarios(HTTP status codes are returned). I'm trying to grasp all these concepts about observables etc but so far I've not reached anywhere. Hope someone could help me on this.
export class HomePage {
private formData: FormGroup;
public errors: string = "";
constructor(
public navCtrl: NavController,
public authService: AuthServiceProvider,
public formBuilder: FormBuilder
) {
this.formData = this.formBuilder.group({
email: ['', Validators.compose([Validators.required,Validators.email])],
password: ['', Validators.required],
});
doRegister(){
this.authService.register(this.formData.value)
//Coming from angular 1 and I've honestly have no idea what I'm doing from here
.toPromise()
.then(data => this.navCtrl.push(ProfiePage,data))
.catch((error: any) => {
/*
I want to show different msgs for different http erros
example
for 500 "username already exists"
for 400 " Invalid username"
for 401 "Someother error"
How do I do this?
*/
this.errors = "Username already exists";
});
}
}
}
@Injectable()
export class AuthServiceProvider {
register(userData): Observable<UserModel[]>{
// http error 500 will be return if there is an email exists error
return this.http.post(this.config.apiUrl+this.config.user.register,JSON.stringify(userData))
.map((res: Response) => res.json());
}
}
<ion-content padding id="page2">
/* I want to show the errors here */
{{error}}
<form [formGroup]="formData" (ngSubmit)="register()">
<ion-list id="signup-list1">
<ion-item id="signup-input5">
<ion-label>
Email
</ion-label>
<ion-input type="email" placeholder="" formControlName="email"></ion-input>
</ion-item>
<ion-item id="signup-input6">
<ion-label>
Password
</ion-label>
<ion-input type="text" placeholder="" formControlName="password"></ion-input>
</ion-item>
</ion-list>
<button [disabled]="!formData.valid" ion-button color="positive" block>
Sign up
</button>
</form>
</ion-content>