When attempting a test, I encountered an issue where the function does not wait for the database request to be made. It seems like the function initially returns undefined, and only after the request is completed it returns true or false causing the test to fail in the else condition.
The challenge lies within the RestService of Angular 4/5.
This function handles password validation in order to execute an update:
handler: data => {
if (this.checkPassword(data.password)) {
console.log("4--------");
// logged in!
let jsonCreate = {
name: form.value.inputName,
email: form.value.inputEmail,
password: form.value.inputPassword,
description: form.value.inputDescription,
id: this.id
};
console.log('jsonCreate');
console.log(jsonCreate);
// add client to the database
this.rest.put('usersClient/atualizacao', jsonCreate)
.subscribe(userUpdated => {
console.log(userUpdated);
});
} else {
console.log("3--------");
return false;
}
}
Password check function:
public checkPassword(password){
console.log("check");
let jsonPost = {};
jsonPost["email"] = this.user.email;
jsonPost["senha"] = password;
console.log(jsonPost);
this.rest.post('Login/clientAuth', jsonPost).subscribe(user => {
console.log("11--------");
if(this.isEmptyObject(user)==true){
console.log("1--------");
return false;
}
else{
console.log("2--------");
return true;
}
});
}