javascriptindexeddb

Uncaught (in promise) TypeError: window.indexedDB.databases is not a function


First time attempting this sort of function and I've tried numerous approaches, but can't figure out what I'm doing incorrectly. I always get an uncaught error... why?

Specifically, I get the following error:

Uncaught (in promise) TypeError: window.indexedDB.databases is not a function

function dbReady(dbName, callback) {
    if(typeof window !== "undefined") {
        (window.indexedDB.databases()).then(dbs => function(dbs) {
            const dbExists = dbs.map(db => db.name).includes(dbName);
            if(dbExists && typeof callback === 'function') { callback(dbExists); return dbExists;}
            else {throw new Error('databases() Failed:');}
        }).catch((e) => {
            console.error(e);
        });
    } else if(typeof callback === 'function') { callback(true);}
};

thank you in advance! ...first time posting on here... if that says something about my level of frustration :)

I tried various approaches using .then() vs try / catch vs. await... I really don't know what I'm doing so just trying what I've come across on this website with others trying to solve for similar situations.


Solution

  • The error happens at

    (window.indexedDB.databases())
    

    It is not caught by catch since it's not thrown inside the promise but the call to create the promise itself. you can either do a null check or surround the whole function body with a try-catch