簡體   English   中英

無法使用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之后,我想將數據傳遞給主題。 我可以看到正在調用數據,但是當我使用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:無法讀取未定義的屬性“ userInfo” TypeError:無法讀取未定義的屬性“ userInfo”

在此處輸入圖片說明

在第118行中,您可以看到console.log顯示正在從indexedDB中提取數據,在第119行中,您可以看到沒有數據正在傳遞並且正在以未定義的方式傳遞。 我期望從indexedDB傳遞數據,以觀察到的方式在其他應用程序中使用。

    db.openDatabase(1).then(function() {

該行更改了this函數內部的內容。 嘗試將其更改為此:

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

可能還有其他問題,我對Angular或可觀察對象一無所知,但這絕對是一個問題。

您可以與BehaviorSubject進行檢查嗎?也許會有所幫助。不要忘記導入它。

public userInfo = new BehaviorSubject<any>('');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM