admin管理员组文章数量:1310203
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: '[email protected]' });
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
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: '[email protected]' });
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
Share Improve this question edited Dec 25, 2018 at 0:36 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Dec 25, 2018 at 0:29 AdamAdam 6321 gold badge10 silver badges23 bronze badges1 Answer
Reset to default 6It looks like you're trying to access db
during the call to add()
when it is uninitialized (note that this call to add()
occurs immediatly when your script is executed).
The db
variable is however only initialized if the database connection is successfully created:
request.onsuccess = function (event)
{
db = request.result;
console.log('The database is opened successfully');
// It is now safe to interact with the database
};
There are number of ways this problem can be addressed though the simplest would be to move your call to add()
into the onsuccess
handler like so:
request.onsuccess = function (event)
{
db = request.result;
console.log('The database is opened successfully');
add(); // Add this
};
// add(); <-- remove this from the end of your script
Finally, I noticed you have two var db;
variables declared - consider removing one of those. Hope this helps!
本文标签: javascriptCannot read property 39transaction39 of undefined IndexedDBStack Overflow
版权声明:本文标题:javascript - Cannot read property 'transaction' of undefined IndexedDB - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741845855a2400775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论