【发布时间】:2019-07-17 10:04:49
【问题描述】:
我在 Angular 5 服务中使用本地存储 (https://github.com/cyrilletuzi/angular-async-local-storage)。首先,我想从存储中加载数据。如果该值不存在,我想从 firebase 加载数据。但是,如果我检查数据是否存在,则无法从服务访问数据库请求...我收到错误TypeError: Cannot read property 'subscribe' of undefined。
PS:这个例子中肯定不存在“user”这个值。
我试过了
getDbFromUser(uid) {
this.localStorage.getItem('user').subscribe((data) => {
if (data) {
console.log(data);
} else {
console.log('Nothing found');
// Load data from Firebase
this.dataObjRef = this.afDatabase.object(`data/users/${uid}`);
this.data = this.dataObjRef.valueChanges();
return this.data;
}
}, () => {
})
}
【问题讨论】:
-
this:
this.localStorage.getItem('user')不返回可观察对象! -
但是如果用户不存在,我会返回一个 Observable...
-
如果它存在那么你返回什么??:>
-
还没有:-)
-
所以你得到了未定义的错误。问题解决了。
标签: angular typescript firebase-realtime-database ionic3 local-storage