I'm using IndexDB to create an object store of users but when trying to add users I get an error on the var request = db.transaction(['person'], 'readwrite') line.
The error given is:
"Uncaught TypeError: Cannot read property 'transaction' of undefined at add (test.js:32) at test.js:45"
My script looks like this:
var request = window.indexedDB.open("connectDB", 1);
request.onerror = function (event)
{
console.log('The database is opened failed');
};
var db;
request.onsuccess = function (event)
{
db = request.result;
console.log('The database is opened successfully');
};
var db;
request.onupgradeneeded = function (event)
{
db = event.target.result;
var objectStore;
if (!db.objectStoreNames.contains('users'))
{
objectStore = db.createObjectStore('users', { keyPath: 'id' });
objectStore.createIndex('name', 'name', { unique: false });
objectStore.createIndex('email', 'email', { unique: true });
}
}
function add()
{
var request = db.transaction(['person'], 'readwrite')
.objectStore('person')
.add({ id: 1, name: 'Jam', age: 24, email: 'jam@example.com' });
request.onsuccess = function (event) {
console.log('The data has been written successfully');
};
request.onerror = function (event) {
console.log('The data has been written failed');
}
}
add();
Any help would be appreciated