【发布时间】:2017-09-01 12:27:00
【问题描述】:
我有一个身份验证服务,我想在它返回之前使用 angularfire 从 firebase 检索用户记录,但数据返回得太晚了。我添加了“.filter(data => data !== undefined)”,但它并没有导致它等待。
this.authState = this.afAuth.authState;
this.authState.subscribe(user => {
if (user) {
this.currentUser = user;
this.userid = this.currentUser.uid;
this.displayname = this.currentUser.displayName;
this.path = '/AU/user/' + this.currentUser.uid + '/';
this.exists = this.af.object(this.path);
// this line of code returns data after the navbar
// and user components have started which is too late.
// The filter was added to try to get it wait for the data.
this.exists.filter(data => data !== undefined).subscribe(x => {
if (x && (x.$value !== null)) {
this.mobile = x.mobile;
this.userid = x.userid;
this.name = x.name;
this.email = x.email;
this.confirm = x.confirm;
// the fields below are undefined in the console log
console.log( 'email', this.email, 'name', this.name)
} else {
this.currentUser = null;
}
});
在谷歌上搜索这个问题时,我发现也许我需要映射响应,但这没有任何区别。下面是我使用的代码
this.exists.map(response => this.response$)
.filter(data => data !== undefined)
.subscribe(x => {
我尝试比较“未定义”和“空”。我的想法已经用完了,请帮忙。
【问题讨论】:
标签: angular asynchronous angular2-services angular2-observables