在使用getByKey从indexedDB调用ngx db之后,我希望将数据传递给主题。我可以看到数据正在被调用,但是当我使用next时,我被告知数据是未定义的。
public userInfo = new Subject<any>();
getData(){
var db = new NgxIndexedDB('jwt', 1);
db.openDatabase(1).then(function() {
db.getByKey('token', 1).then(
(res) => {
// Do something after the value was added
console.log(res); //data populates in the console
this.userInfo.next(res) // this step I receive undefined error
},
error => {
console.log(error);
}
);
}
watchUser(): Observable<any> {
return this.userInfo.asObservable();
};
getUserInfo(){
this.watchUser().subscribe(res => console.log(res))
}错误:未知(承诺):TypeError:无法读取未定义TypeError的属性“userInfo”:无法读取未定义的属性“userInfo”

在第118行中,您可以看到console.log显示数据是从indexedDB中提取出来的,在第119行中,您可以看到没有数据被传递,并且正在以未定义的方式通过。我希望传递来自indexedDB的数据,以便在应用程序的其余部分中使用。
发布于 2019-08-22 16:07:07
请您检查一下BehaviorSubject好吗?也许会有帮助的。别忘了导入它。
public userInfo = new BehaviorSubject<any>('');https://stackoverflow.com/questions/57613220
复制相似问题