“Cannot call method 'open' of undefined” error when using indexedDB.open

I am new to IndexedDB and I am following this guide IndexedDB Tutorial I am simply trying to create a database and then be able to add a few entries. This is what I have so far.

var db = window.indexedDB.open('FriendDB', 'My Friends!');

if (db.version != '1') {
    // User's first visit, initialize database (name, key, auto increment).
    db.createObjectStore('Friends', 'id', true);
    db.setVersion('1');
} else {
  // DB already initialized.
}

var store = db.openObjectStore('Friends');
var user = store.put({name: 'Eric', gender: 'male', likes: 'html5'});

In my console I get the error "Cannot call method 'open' of undefined" how can I get this working? Also if there is a better resource online that would help me because I can't seem to find anything on the topic of IndexedDB for a newbie.

Answers:

Answer

Here is the indexeddb demo from html5rocks which i have improved to work on Mozilla Firefox and added features for viewing details data and editing existing data. Inside you have explanations how to create db, insert, update and delete data in indexeddb.

https://github.com/denimf/IndexedDbToDo

Answer

Any time you see code containing a call to setVersion, it's using an outdated syntax. It was unfortunate that we had to make such a big change so late during the spec writing, but it made using IndexedDB correctly tremendously simpler so we decided it was worth it.

There is good documentation on developer.mozilla.org, even though it could definitely be improved.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.