Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IndexedDB版本和Dexie.js出现错误_Javascript_Database_Client_Indexeddb_Dexie - Fatal编程技术网

Javascript IndexedDB版本和Dexie.js出现错误

Javascript IndexedDB版本和Dexie.js出现错误,javascript,database,client,indexeddb,dexie,Javascript,Database,Client,Indexeddb,Dexie,我从IndexedDB开始,为了不重新发明轮子,我使用了Dexie.js 我创建了数据库,添加了数据,现在我正在创建一个通用函数,它可以获取CSV并将数据库填充到其他表中 所以,我的代码或多或少是 // Creation and populate database and first table var db = new Dexie("database"); db.version(1).stores({table1: '++id, name'}); db.table1.add({name: '

我从IndexedDB开始,为了不重新发明轮子,我使用了Dexie.js

我创建了数据库,添加了数据,现在我正在创建一个通用函数,它可以获取CSV并将数据库填充到其他表中

所以,我的代码或多或少是

// Creation and populate database and first table
var db = new Dexie("database");
db.version(1).stores({table1: '++id, name'});

db.table1.add({name: 'hello'});
直到这里一切都好

现在,ajax请求成功了

db.close();
db.version(2).stores({table2: '++id, name'});
db.open();

db.table2.add({name: 'hello'});
这段代码第一次运行时一切正常,但下一次我遇到这个错误

VersionError The operation failed because the stored database is a 
higher version than the version requested.
若我删除数据库并再次运行代码,那个么只有在第一次运行时才行

有什么想法吗?我不喜欢太多IndexedDB版本的方式,它看起来令人沮丧,而且我在网上也没有得到很多帮助 谢谢

编辑: 我发现了问题/错误/程序?。如果在任何版本修改之前我没有添加任何内容,我就没有这个问题,但是有人知道这是否是正常的过程吗

所以。。如果这是一个过程,我不能用一个泛型方法平日添加任何表。首先是所有声明,然后添加值。是否可以在添加值后添加表

再次编辑。。。我刚刚意识到我可以创建另一个数据库。我会发布结果。但欢迎提供有关此问题的任何信息:)


再次编辑。。。我创建了另一个数据库,每个人都很高兴

这是因为代码第二次运行时,您的数据库在版本2上,但您的主代码仍试图在版本1上打开它

如果不知道安装的当前版本,请尝试在动态模式下打开dexie。这是通过不指定任何版本来实现的:

var db = new Dexie('database');
db.open().then(function (db) {
    console.log("Database is at version: " + db.verno);
    db.tables.forEach(function (table) {
        console.log("Found a table with name: " + table.name);
    }); 
});
要动态添加新表,请执行以下操作:

function addTable (tableName, tableSchema) {
    var currentVersion = db.verno;
    db.close();
    var newSchema = {};
    newSchema[tableName] = tableSchema;

    // Now use statically opening to add table:
    var upgraderDB = new Dexie('database');
    upgraderDB.version(currentVersion + 1).stores(newSchema);
    return upgraderDB.open().then(function() {
        upgraderDB.close();
        return db.open(); // Open the dynamic Dexie again.
    });
}
后一个函数返回一个承诺,在使用新表之前等待它完成

如果您的应用驻留在多个浏览器中,其他窗口也将关闭其db连接,因此它们永远不会相信db实例在任何时候都是打开的。您可能希望侦听db.on('versionchange')()以覆盖该数据库的默认行为:

db.on("versionchange", function() {
    db.close(); // Allow other page to upgrade schema.
    db.open() // Reopen the db again.
        .then(()=> {
           // New table can be accessed from now on.
        }).catch(err => {
           // Failed to open. Log or show!
        });
    return false; // Tell Dexie's default implementation not to run.
};