【发布时间】:2016-09-26 18:38:30
【问题描述】:
我在我的网站上进行了身份验证,它工作正常,但我想处理错误,为了测试,我为此目的导致了一个来自 firebase 的返回:“一个帐户已经存在,具有相同的电子邮件地址但不同的符号-在凭据中。使用与此电子邮件地址关联的提供商登录。"。我正在尝试使用以下代码编写一个可观察的 RXjs 来做到这一点:
private subscribleAuth() {
this.af.auth
.subscribe(
auth => {
this._auth = auth && auth.auth ? auth.auth : null;
if (this._auth) {
this.loginState = 'logged';
} else {
this.loginState = 'login';
}
},
err => {
console.log('error'),
this.loginState = 'error';
},
() => { console.log('done.') }
);
}
我的控制台从不显示“错误”或“完成”,所以我放入错误代码中的任何内容都不起作用。
注意:我知道导致 firebase 返回的原因,我只是想知道如何处理它以及为什么我编写的代码没有按预期工作,当没有返回错误时,该站点在相同的 observable 下按预期工作。
我正在使用 angularfire2: ^2.0.0-beta.5 和 Angular 2.0.0 final
编辑 2:reported as an issue,也许这真的是一个错误,我会继续更新这篇文章。现在的解决方法是使用 yashmurty 在 github 上编写的代码:
import { AngularFire, AuthProviders } from 'angularfire2';
import { AuthBackend } from 'angularfire2/auth/auth_backend';
export class SocialmenuComponent {
constructor(public af: AngularFire, private _authBackend: AuthBackend) {
// this.af.auth.subscribe(
this._authBackend.getRedirectResult().subscribe(
(auth) => {
if(auth) {
console.log(auth);
} else {
console.log('User Not Logged In');
}
},
(err) => {
if(err) {
console.log('Error in auth.subscribe : ');
console.log(err);
} else {
console.log('No Error detected in auth.subscribe');
}
},
() => { console.log('done.') }
);
}
}
【问题讨论】:
-
你是说这条线
this.af.auth.subscribe(auth => console.log(auth));?但这不是你在做什么。查看最底部:github.com/angular/angularfire2/blob/master/docs/… 他们将.then()和.catch()绑定在auth().signInWithCredential(provider)返回的Promise 上。这就是您可以捕获错误的方式。 -
这是 apache cordova 的情况,如果我使用 popup 方法也应该可以工作,我使用的是 redirect 方法,因此页面重定向到提供程序页面,丢失内存上的所有变量/状态(和then 函数在重定向之前被调用),当它返回我的页面时,如果我调用 auth.login 将再次在无限循环中重定向到提供程序页面...
-
我可以捕捉到这样的错误,但我仍然在 error_handler.js 和 Subscriber.js 中抛出异常——它们似乎破坏了程序的执行——有什么方法可以使这些错误静音/忽略?
-
我不知道,这只是一个解决方法,从现在开始我使用弹出方法,直到他们解决了问题。
标签: rxjs angularfire2