Issue
Hi i am trying to get the error return code to my calling function inside ionic, but I am not able to return the error from the function called.
Here is my main page where the function is called:
let result:any = await this.authService.SignUp(
this.registerForm.value["name"],
this.registerForm.value["phone"],
this.registerForm.value["email"],
this.registerForm.value["password"],
this.attorneyId
);
if (result && result.code) {
console.log('Register Page Error', result.code); //NOT GETTING RESULT HERE
this.alertService.showErrorMessage(result.code);
}
here is my function which is called from above:
async SignUp(
fullName: string,
phone: number,
email: string,
password: string,
attid: number
) {
console.log("Inside Auth SignUp");
this.afDatabase.list("/attorneyList", ref =>
ref.orderByChild("attid").equalTo(attid)
).valueChanges().subscribe(async data => {
console.log("Inside Data")
if (data[0]) {
return await this.afAuth
.createUserWithEmailAndPassword(email, password)
.then(async result => {
console.log("Signup Result", result)
result.user["fullName"] = fullName;
result.user["phone"] = phone;
result.user["attid"] = attid;
await this.SetUserData(result.user);
this.navCtrl.navigateRoot(["/tabs"]);
})
.catch(error => {
console.log("Signup Error", error); //GETTING ERROR HERE
return error; //THIS IS NOT RETURNING THE ERROR BACK TO THE ABOVE CALLING FUNCTION
});
}
});
}
Any idea what am I doing wrong here??? Please help.
Solution
The return
inside your subscribe
call on the firestore query does not actually return that data from the function. you could, however, convert your query into a promise by calling .valueChanges().pipe(first()).toPromise()
and await
the result of that query. then the code, that is currently inside your subscribe
call should work as expected.
async SignUp(
fullName: string,
phone: number,
email: string,
password: string,
attid: number
) {
console.log("Inside Auth SignUp");
const data = await this.afDatabase.list("/attorneyList", ref =>
ref.orderByChild("attid").equalTo(attid)
).valueChanges().pipe(first()).toPromise();
console.log("Inside Data")
if (data[0]) {
return await this.afAuth
.createUserWithEmailAndPassword(email, password)
.then(async result => {
console.log("Signup Result", result)
result.user["fullName"] = fullName;
result.user["phone"] = phone;
result.user["attid"] = attid;
await this.SetUserData(result.user);
this.navCtrl.navigateRoot(["/tabs"]);
})
.catch(error => {
console.log("Signup Error", error); //GETTING ERROR HERE
return error; //THIS IS NOT RETURNING THE ERROR BACK TO THE ABOVE CALLING FUNCTION
});
}
}
That code should demonstrate my solution
Answered By - Isigiel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.