无法使用 ngx-indexed-db 将数据从 indexedDB 传递到 Angular Subject 以便在 Observable 中使用

Unable to pass data from indexedDB to an Angular Subject for use in an Observable using ngx-indexed-db

在我使用 indexedDB 中的 ngx-indexed-db 调用 getByKey 之后,我想将数据传递给主题。我可以看到正在调用数据,但是当我接下来使用时,我被告知数据未定义。

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))
}

ERROR 错误:未捕获(承诺):TypeError:无法读取未定义的 属性 'userInfo' 类型错误:无法读取未定义的 属性 'userInfo'

在第 118 行中,您可以看到 console.log 显示正在从 indexedDB 中提取数据,在第 119 行中,您可以看到没有数据被传递,并且以未定义的形式传递。我期望从 indexedDB 传递数据以供 observable 用于应用程序的其余部分。

你能检查一下 BehaviorSubject 吗?也许会 helpful.Don不要忘记导入它。

public userInfo = new BehaviorSubject<any>('');
    db.openDatabase(1).then(function() {

该行更改了该函数中 this 的内容。尝试将其更改为:

    db.openDatabase(1).then(() => {

可能还有其他问题,我对 Angular 或可观察对象一无所知,但这绝对是一个问题。