Javascript,IndexedDB,存储 Uint8Array,报错
Javascript, IndexedDB, storing Uint8Array, Giving Error
我开始阅读 IndexedDB 已经有几个小时了,我遇到了一些问题。这是我遵循 https://medium.com/@AndyHaskell2013/build-a-basic-web-app-with-indexeddb-8ab4f83f8bda and https://gist.github.com/JamesMessinger/a0d6389a5d0e3a24814b 的代码,只是为了便于理解而将其拼凑在一起。所以我想做的是将密钥存储到 IndexedDB 中,以便我可以使用它通过 ECIES 方案加密内容。
相关代码(顺序):
var secretKey = eccrypto.generatePrivate();
window.genSKey = function()
{
openDB();
console.log(secretKey);
return;
}
window.openDB = function()
{
let db;
let dbReq = indexedDB.open('ln', 1);
dbReq.onupgradeneeded = function(event) {
// Set the db variable to our database so we can use it!
db = event.target.result;
// Create an object store named notes. Object stores
// in databases are where data are stored.
let skey = db.createObjectStore('skey', {keyPath: "id"});
}
dbReq.onsuccess = function(event) {
db = event.target.result;
setSkey(db);
getSkey(db);
}
dbReq.onerror = function(event) {
alert('error opening database ' + event.target.errorCode);
}
}
window.setSkey = function(db) {
// Start a database transaction and get the notes object store
let tx = db.transaction(['skey'], 'readwrite');
let store = tx.objectStore('skey');
// Put the sticky note into the object store
store.put({id: 1, secretKey});
// Wait for the database transaction to complete
tx.oncomplete = function() { console.log('private key was saved') }
tx.onerror = function(event) {
alert('error storing private key ' + event.target.errorCode);
}
}
window.getSkey = function(db)
{
// Set up an object store and transaction
let tx = db.transaction(['skey'], 'readonly');
let store = tx.objectStore('skey');
// Set up a request to get the sticky note with the key 1
let req = store.get(1);
// We can use the note if the request succeeds, getting it in the
// onsuccess handler
req.onsuccess = function(event) {
let skey = event.target.result;
if (skey) {
console.log(skey);
} else {
console.log("skey 1 not found")
}
}
// If we get an error, like that the note wasn't in the object
// store, we handle the error in the onerror handler
req.onerror = function(event) {
alert('error getting note 1 ' + event.target.errorCode);
}
出于某种原因,当在前端脚本中调用 genSKey 时,我收到以下控制台错误:
Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
at window.setSkey (http://127.0.0.1:5500/browserify/builds/genKey.js:26763:17)
at IDBOpenDBRequest.dbReq.onsuccess (http://127.0.0.1:5500/browserify/builds/genKey.js:26752:3)
window.setSkey @ genKey.js:26763
dbReq.onsuccess @ genKey.js:26752
IndexedDB (async)
window.openDB @ genKey.js:26740
window.genSKey @ genKey.js:26601
signupData @ signup.html:186
onclick @ signup.html:64
控制台记录了更多内容,但其他内容与手头的问题无关。我不确定我做错了什么,我整天都在做这件事。一些洞察力对我来说意义重大,谢谢。
您创建的数据库可能没有对象存储。要触发 onupgradeneeded
,它将去创建对象存储,您必须连接到具有更高版本号的数据库。
我开始阅读 IndexedDB 已经有几个小时了,我遇到了一些问题。这是我遵循 https://medium.com/@AndyHaskell2013/build-a-basic-web-app-with-indexeddb-8ab4f83f8bda and https://gist.github.com/JamesMessinger/a0d6389a5d0e3a24814b 的代码,只是为了便于理解而将其拼凑在一起。所以我想做的是将密钥存储到 IndexedDB 中,以便我可以使用它通过 ECIES 方案加密内容。
相关代码(顺序):
var secretKey = eccrypto.generatePrivate();
window.genSKey = function()
{
openDB();
console.log(secretKey);
return;
}
window.openDB = function()
{
let db;
let dbReq = indexedDB.open('ln', 1);
dbReq.onupgradeneeded = function(event) {
// Set the db variable to our database so we can use it!
db = event.target.result;
// Create an object store named notes. Object stores
// in databases are where data are stored.
let skey = db.createObjectStore('skey', {keyPath: "id"});
}
dbReq.onsuccess = function(event) {
db = event.target.result;
setSkey(db);
getSkey(db);
}
dbReq.onerror = function(event) {
alert('error opening database ' + event.target.errorCode);
}
}
window.setSkey = function(db) {
// Start a database transaction and get the notes object store
let tx = db.transaction(['skey'], 'readwrite');
let store = tx.objectStore('skey');
// Put the sticky note into the object store
store.put({id: 1, secretKey});
// Wait for the database transaction to complete
tx.oncomplete = function() { console.log('private key was saved') }
tx.onerror = function(event) {
alert('error storing private key ' + event.target.errorCode);
}
}
window.getSkey = function(db)
{
// Set up an object store and transaction
let tx = db.transaction(['skey'], 'readonly');
let store = tx.objectStore('skey');
// Set up a request to get the sticky note with the key 1
let req = store.get(1);
// We can use the note if the request succeeds, getting it in the
// onsuccess handler
req.onsuccess = function(event) {
let skey = event.target.result;
if (skey) {
console.log(skey);
} else {
console.log("skey 1 not found")
}
}
// If we get an error, like that the note wasn't in the object
// store, we handle the error in the onerror handler
req.onerror = function(event) {
alert('error getting note 1 ' + event.target.errorCode);
}
出于某种原因,当在前端脚本中调用 genSKey 时,我收到以下控制台错误:
Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
at window.setSkey (http://127.0.0.1:5500/browserify/builds/genKey.js:26763:17)
at IDBOpenDBRequest.dbReq.onsuccess (http://127.0.0.1:5500/browserify/builds/genKey.js:26752:3)
window.setSkey @ genKey.js:26763
dbReq.onsuccess @ genKey.js:26752
IndexedDB (async)
window.openDB @ genKey.js:26740
window.genSKey @ genKey.js:26601
signupData @ signup.html:186
onclick @ signup.html:64
控制台记录了更多内容,但其他内容与手头的问题无关。我不确定我做错了什么,我整天都在做这件事。一些洞察力对我来说意义重大,谢谢。
您创建的数据库可能没有对象存储。要触发 onupgradeneeded
,它将去创建对象存储,您必须连接到具有更高版本号的数据库。