1

I need to check if some table already exists into IndexedDB just after it was openned. But I don't know how to get DexieDB object inside 'then' statement.

this.db = new Dexie("DBNAME");
if (!this.db.isOpen()) {
    this.db.open().then(function () {
        //how to get this.db.table(storeName) here?
    }).catch(function (error) {
        console.log(error)
    });
}

So this.db doesn't exist inside 'then' statement. How to get it?

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
splash27
  • 2,057
  • 6
  • 26
  • 49

1 Answers1

2

In Dexie

In Dexie in particular you don't have to call isOpen and open() like that, you can just .open and things will work like this:

// Declare db instance
var db = new Dexie("MyDatabase");

// Define Database Schema
//...
// Open Database
db.open(); 

db.trasnaction(...

In General

This is a classic JS context value. The way this works in JavaScript is different - here is the canonical reference about it which you should read.

In addition - about passing parameters in then chains you should refer to this excellent Q&A which covers the more general approach

The workarounds described there (with context) generally apply and contain more library specific code which could help you here.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504