正确初始化IndexedDB对象存储

06odsfpq  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(244)

我试图使用indexedDB来存储一些数据客户端,但在正确设置数据库时遇到了麻烦。
我正在遵循教程here
当我第一次安装扩展时,onupgradeneeded()事件****被激发,但是由于某种原因,没有任何与对象存储事务相关的事件被激发,并且我找不到解决此问题的任何文档。
以下是相关代码:

const db_name="Tags";

var request = window.indexedDB.open(db_name, 1);
var tags  = [
    //codes: 0 - markdown wrap tag
    //       1 - HTML wrap tag 
    //       2 - single tag
    { domain: "www.youtube.com", 
      
      bold:["*",0],
      strikethrough:["-",0],
      italic:["_",0] 
    },
    
    
    { domain: "www.stackoverflow.com", 
      
      bold:["<strong>",1], 
      italic:["<em>",1],
      strikethrough:["<del>",1],
      superscript:["<sup>",1],
      subscript:["<sub>",1],
      heading1:["<h1>",1],
      heading2:["<h2>",1],
      heading3:["<h3>",1],
      blockquote:["<blockquote>",1],
      code:["<code>",1],
      newline:["<br>",2],
      horizontal:["<hr>",2]
    }
];

request.onerror = function(event) {
  alert("Error opening the database");
};

request.onupgradeneeded = function(event) {
    var db = event.target.result;
    var objectStore = db.createObjectStore("domains", {keyPath: "domain" });
    objectStore.createIndex("domain", "domain", { unique: true });
    alert("I'm trying to do stuff"); 
    objectStore.transaction.oncomplete = function(event) {
        var domanStore=db.transaction("domains","readwrite").objectStore("domains");
        for(var i in tags)
        {
            domainStore.add(tags[i]);
            alert("added " + tags[i]["domain"] + " to the DB");
        }
        alert("I might have done stuff");
    };
    objectStore.transaction.onerror = function(event) {
        alert("Transaction error");
    };
    objectStore.transaction.onabort = function(event){
        alert("Transaction aborted");
    };


    db.onerror=function(event){
        alert("DB ERROR bubbled up");
    };
    alert("I thought I did something");
};

唯一弹出的警告框是
我在努力做事。

我以为我做了什么。
控制台未显示错误。
我唯一能想到的是我没有正确地创建对象存储,但我看不出我在这里做错了什么。
其他一些有用的资源:

rqcrx0a6

rqcrx0a61#

你可以用这样的东西-

const db_name="Tags";

var request = window.indexedDB.open(db_name, 1);
var tags  = [
    //codes: 0 - markdown wrap tag
    //       1 - HTML wrap tag 
    //       2 - single tag
    { domain: "www.youtube.com", 

      bold:["*",0],
      strikethrough:["-",0],
      italic:["_",0] 
    },

    { domain: "www.stackoverflow.com", 

      bold:["<strong>",1], 
      italic:["<em>",1],
      strikethrough:["<del>",1],
      superscript:["<sup>",1],
      subscript:["<sub>",1],
      heading1:["<h1>",1],
      heading2:["<h2>",1],
      heading3:["<h3>",1],
      blockquote:["<blockquote>",1],
      code:["<code>",1],
      newline:["<br>",2],
      horizontal:["<hr>",2]
    }
];

request.onerror = function(event) {
  alert("Error opening the database");
};

request.onupgradeneeded = function(event) 
{
   var db = event.target.result;
   var objectStore = db.createObjectStore("domains", {keyPath: "domain" });
   objectStore.createIndex("domain", "domain", { unique: true });
   alert("I'm trying to do stuff"); 
   for(var i in tags)
   {
       objectStore.add(tags[i]);
       alert("added " + tags[i]["domain"] + " to the DB");
   }
   alert("I might have done stuff");

   alert("I thought I did something");
};

由于您是在创建objectStore时填充数据,因此不必在onupgradeneded函数中创建事务,在此函数之外,您必须创建事务

相关问题