When using ngx-indexed-db's getByKey method to retrieve data from indexedDB and passing it to the subject, there seems to be an issue. Although the data is successfully retrieved, an error occurs stating that the data is undefined when attempting to use next.
public userInfo = new Subject<any>();
getData(){
var db = new NgxIndexedDB('jwt', 1);
db.openDatabase(1).then(function() {
db.getByKey('token', 1).then(
(res) => {
// Perform operations after data retrieval
console.log(res); // Data appears in the console
this.userInfo.next(res) // The error of receiving undefined happens at this step
},
error => {
console.log(error);
}
);
}
watchUser(): Observable<any> {
return this.userInfo.asObservable();
};
getUserInfo(){
this.watchUser().subscribe(res => console.log(res))
}
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'userInfo' of undefined TypeError: Cannot read property 'userInfo' of undefined
https://i.sstatic.net/oXXlg.png
The issue arises whereby data retrieved from indexedDB is visible in the console output but becomes undefined when trying to pass it to the observable to be used throughout the application. Line 119 illustrates this discrepancy and the goal was to utilize the indexedDB data for various purposes within the application.