【发布时间】:2017-05-09 05:31:27
【问题描述】:
我查看了许多资源,包括this、this 和this,但我一直无法达到预期的效果。
我要做的就是对用户进行身份验证(使用 firebase),然后在通过身份验证后,加载他们的个人资料并将其存储在变量 userProfile 中,然后再加载下一页仪表板:
我的登录服务:
public signinUser(user: User) {
this.af.auth.login({
email: user.email,
password: user.password
},
{
provider: AuthProviders.Password,
method: AuthMethods.Password
}
)
.then(
success => {
console.log('Authenticated');
this.getProfile().subscribe( // PROBLEM is here
profile => {
console.log(profile);
console.log('Profile Loaded');
this.router.navigate(['/dashboard']);
}
)
}
)
.catch(function (error) {
...
console.log('ERROR SIGNING USER IN');
}
我的getProfile() 方法:
public getProfile(): Observable<any> {
return this.af.database.object('/profiles/' + this.user.uid)
.flatMap(
profile => {
console.log('inside success');
console.log(profile);
this.userProfile = <User>profile;
console.log('getProfile has completed');
return profile;
},
error => {
console.log(error)
});
这是部分日志的外观:
Authenticated
auth.service.ts:104 ERROR SIGNING USER IN
auth.service.ts:105 TypeError: Cannot read property 'subscribe' of undefined
at auth.service.ts:91
at ZoneDelegate.invoke (zone.js:232)
at Object.onInvoke (ng_zone.js:238)
at ZoneDelegate.invoke (zone.js:231)
at Zone.run (zone.js:114)
at zone.js:502
at ZoneDelegate.invokeTask (zone.js:265)
at Object.onInvokeTask (ng_zone.js:229)
at ZoneDelegate.invokeTask (zone.js:264)
at Zone.runTask (zone.js:154)
auth.service.ts:106 Cannot read property 'subscribe' of undefined
auth.service.ts:184 inside success
auth.service.ts:185 Object {confirmPassword: "123123", email: "aaa@gmail.com2", github: "aaa" name: "a", password: "123123"…}
auth.service.ts:188
getProfile has completed()
auth.service.ts:185
我可以看到这些方法单独按预期工作。用户进行身份验证,并加载配置文件(显示在日志中)。问题在于订阅事件,原因我不知道。
【问题讨论】:
标签: angular asynchronous callback observable chaining