indexeddb 调用堆栈的无效状态错误

indexeddb Invalid state error by call-stack?

本文关键字:状态 错误 无效 调用 堆栈 indexeddb      更新时间:2023-09-26

再次,我在indexeddb上遇到了一些问题。我得到了一个

无效状态错误: 尝试对数据库执行更改操作 这不允许突变。

还有一个

AbortError

这是我的代码:

DB_LINK.prototype.pushStoreNumeric = function () 
{ 
    // Saving Values 
    var _temp = 0; 
    var _version = this.link.version; 
    var _name = this.link.name; 
    var that = this; 
    var _objectStoreNames = this.link.objectStoreNames; 
    // Close DB 
    this.link.close(); 
    this.state = 4; 
    // Reopen Database    
    this.req = indexedDB.open(_name,_version+1); // Abort error here
    this.req.onupgradeneeded = function () { 
    that.state = 1; 

    // Get Number of object stores 
    _temp = _objectStoreNames.length; 
    if(_temp != 0) 
    { 
        // Already object stores: read highest value 
        _temp = parseInt(_objectStoreNames[_objectStoreNames.length - 1]); 
    } 
    that.link.createObjectStore(_temp); // InvalidStateError here
}; 

我已经标记了每条评论发生错误的位置。

首先发生无效

状态错误,然后发生中止错误。

我在同一数据库的另一个成功函数中调用此函数。这可能是问题所在吗?

什么是this.link?这可能是问题所在。您需要对indexedDB.open请求创建的数据库实例执行createObjectStore。所以无论是this.req.result.createObjectStore还是(如果你改成this.req.onupgradeneeded = function (e) {)你可以使用e.target.result.createObjectStore.

更一般地说,我无法真正评论您的代码应该做什么,因为我只能看到一个片段,但是每次调用它时您如何递增版本看起来真的很奇怪。可能你实际上并不想这样做。您可能想阅读更多文档。