你好,我想在ionic内部获取调用函数的错误返回代码,但我无法从调用的函数中返回错误。
这里是我调用函数的主页面。
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);
}
这是我的函数,从上面调用。
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
});
}
});
}
知道我在这里做错了什么吗?请帮助我。
该 return
在你 subscribe
调用firestore查询实际上并不能从函数中返回数据。然而,你可以通过调用 .valueChanges().pipe(first()).toPromise()
和 await
的结果。然后,当前在你的应用程序中的代码。subscribe
调用应该可以正常工作。
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
});
}
}
这段代码应该可以证明我的解决方案