I might sound dumb but I'm a newbie in angular2. I'm working on a project on ionic2, I've written an auth-service that has a function IsEmailAvailable() for async validation on input tag.
I'm unable to fire http.post request. I've tried many tutorials. I know http in angular2 return Observable which has many advantages. I've tried converting the http.post to .toPromise and tried using .then too. Still no progress.
this.http.post(MyApp.BASE_URL+"/api/auth/email",body).map(res => res.json())
.subscribe(
res => {console.warn("some thing")},
err => { console.error("some bullshit")},
() => {console.log('Authentication Complete')}
);
EDIT 1
public isEmailAvailable(control : Control) {
return new Promise(resolve => {
let body = JSON.stringify({username: control.value});
console.log(body);
let headers = new Headers({ 'Content-Type': 'application/json' });
this.http.post(MyApp.BASE_URL+"/api/auth/email",body).map(res => res.json())
.subscribe(
res => {console.warn("some thing")},
err => { console.error("some bullshit")},
() => {console.log('Authentication Complete')}
);
});
}
EDIT 2 Just to add more insight to this. I'm using this isEmailAvailable function in a provider and using it as an async validator like this in my signup.ts page :
constructor(private navCtrl: NavController, formBuilder : FormBuilder,
public globalVars : GlobalVars, public userData: UserData, public
authService : AuthService)
{
this.nav = navCtrl;
this.signUpForm = formBuilder.group({
email : [
'',Validators.compose([]),authService.isEmailAvailable],
password: ['',Validators.compose([
Validators.maxLength(100),
Validators.minLength(6),
Validators.required,
Validators.pattern(globalVars.getPasswordPattern().toString())])]
});
}
this.http.post(...)is called at all? - Günter Zöchbauer