I am trying to create a login page that fetches user data from server and show error message for wrong login/password the code looks like this:
login(x) {
// alert(this.form.value)
this.errmsg = "";
if (x.uid == "" || x.pwd == "") {
this.errmsg = "Fields should not be empty";
}else{
// parameter to pass on class
var params={
myclass:this,
}
// Getrequest to get element by id
var GetUserById = {
method: 'GET',
url: 'http://192.168.136.136:8100/User/' + x.uid + '/',
headers:
{
'Postman-Token': 'a81c98b6-5921-4ab5-bfbc-27dcff4b36ea',
'Cache-Control': 'no-cache'
}
}
var check=(body, x) =>{
body=JSON.parse(body)
if (body.length == 0) {
return false
}
else {
var uinfo = body[0]
if (uinfo.Password == x.pwd) {
window.sessionStorage.setItem('username', uinfo.UserId);
window.sessionStorage.setItem('Role', this.defineRole(uinfo.Role));
// window.sess
return true
}
else{
return false
}
}
}
var showError=() =>{
params.myclass.errmsg= "Invalid Username or Password"
}
request(GetUserById, function (error, response, body) {
if (error) throw new Error(error);
if(check(body, x)){
params.myclass.navCtrl.push(Home, {});
}
// createVerify
else{
showError()
return
}
});
}
}
html :
<form [formGroup]="form" (ngSubmit)="login(form.value)">
<ion-list>
<ion-item>
<ion-input type="text" formControlName="uid" placeholder="Username"></ion-input>
</ion-item>
<ion-item>
<ion-input type="password" formControlName="pwd" placeholder="Password"></ion-input>
</ion-item >
</ion-list>
<p id="almsg" class="text-right small" >{{errmsg}}</p>
<div padding>
<button ion-button type="submit" color="basic" block >Sign In</button>
</div>
</form>
the errmsg in showerror() is not affecting the html page unless I tap on one of the input. I tried console.log but that is working perfectly fine,
Try this, I guess you are not updating the class level variable errmsg
with error message.
var showError=() =>{
//params.myclass.errmsg= "Invalid Username or Password";
this.errmsg = "Invalid Username or Password";
}
try this
var showError=() =>{
this.errmsg = "Invalid Username or Password";
}
request(GetUserById, (error, response, body) => {
if (error) throw new Error(error);
if(this.check(body, x)){
this.navCtrl.push(Home, {});
}
// createVerify
else{
this.showError()
return
}
});
First of all params.myclass.errmsg and this.errmsg are not same you need to change params.myclass.errmsg to this.errmsg then it will be show
Modify sohowError
and request
var showError=() =>{
this.errmsg= "Invalid Username or Password"
}
request(GetUserById, function (error, response, body) {
if (error) throw new Error(error);
if(check(body, x)){
params.myclass.navCtrl.push(Home, {});
}
// createVerify
else{
showError();
return false;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.