Currently, I am working on a basic Angular project and I have the requirement to save some data in IndexedDB.
In my service, there is an initialization of private db: IDBDatabase;
within the constructor:
However, I face an issue where this initialized DB is not defined when I try to use it in another method call after the constructor. It seems like the problem might be related to asynchronous calls, callbacks, and promises but I'm having trouble pinpointing the exact cause ... so for now, my temporary workaround is to manually call
window.indexedDB.open("MyTestDatabase", 1);
whenever needed
import {Injectable} from '@angular/core';
import {Directory, Bookmark} from "./models";
import {connectableObservableDescriptor} from "rxjs/internal/observable/ConnectableObservable";
@Injectable({
providedIn: 'root'
})
export class BookmarksService {
private db: IDBDatabase;
private directoriesStoreName = "directories";
private bookmarksStoreName = "bookmarks";
// Rest of the code remains the same ...
Additionally, here is how I am using a component:
export class DirectoriesListComponent implements OnInit {
@Input() bookmarks: Bookmark[];
@Input() directories: Directory[];
isCollapsed = false;
common: CommonService;
bookmarksService: BookmarksService;
// Constructor definition and ngOnInit implementation continues...