Search code examples
javascriptindexeddbdexie

Dexie.exists() method returns nothing (Dexie.js & IndexedDB)


So I'm building a small app with offline support using dexie.js and IndexedDB for the first time. If a database already exists on the local machine, I want to open and use that existing database-->so I check to see if one already exists using Dexie.exists(). Borrowing the sample code from the Dexie.js wiki, I've added the following to my app (rather than logging to console, I'm testing with alerts)

Dexie.exists("myDatabase").then(function(exists) {
  if (exists)
    alert("Database exists");
  else
    alert("Database doesnt exist");
}).catch(function (error) {
    alert("Oops, an error occurred when trying to check database existence");
});

However, this code--which is taken directly from the sample--does not work for me. If the database does not exist, the code correctly returns the alert "Database doesn't exist". However, while testing, I previously created a database and then deleted it using db.delete(). Now, when I test for the existence of the database I deleted, nothing is returned. It should return "Database doesn't exist", or at least an error, but it doesn't. I've found that if I update the code to the following:

Dexie.exists("myDatabase").then(function(exists) {
  if (exists)
    alert("Database exists");
  else
    alert("Database doesnt exist");
}).catch(
    alert("Oops, an error occurred when trying to check database existence")
);

the alert "Oops, an error occurred when trying to check database existence" DOES pop up. Unfortunately, I have no idea what the error is and I have no idea why the catch() statement is only working when I remove the anonymous function.

Looking at the code for Dexie.exists(), it tests to see if it can open() the database in question. As far as I can tell, trying to open that database I deleted seems to fail in a similar manner to how Dexie.exists() is failing for me. It seems like maybe I messed up something while trying to delete the database previously, and now Dexie isn't working. Because of how well Dexie incorporates promises, I can't see any of the errors that I'm assuming are being generated.

I've spent about 8 hours trying to solve this now to no avail. If anyone has experience using Dexie.js and can point me in the right direction, I would VERY much appreciate it. Alternatively, if someone has experience with IndexedDB, perhaps I may be able to solve the problem by 'resetting' the local IndexedDB (if I did in fact screw something up while testing). I haven't been able to figure out a way to do this though (as far as I can tell, working with IndexedDB directly is painful).

SOOOO much thanks for any help with this!!


Solution

  • So your .catch(alert()) is returning a false negative... It will show an alert even if the promise resolves... From what you have said it would appear the promise is not resolving and is thus remaining in a pending state. Clearing the cache of the browser will help. Like This