zoukankan      html  css  js  c++  java
  • HTML5-indexedDB使用常见错误总结

    indexedDB使用过程中的错误1:

    Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not running a version change transaction.

    这是由于你在success事件的回调中调用createObjectStore方法,该方法应该在upgradeneeded事件的回调中调用。

    \获取indexedDB对象
    const indexedDB = window.indexedDB || window.webkitIndexedDB || 
    window.mozIndexedDB;
    \打开数据库,在回调中创建store object
    const request = indexedDB.open(DBName, version);
    request.onsuccess = (e) => {
    this.db = e.target.result;
    };
    request.onupgradeneeded = (e) => {
    this.db = e.target.result;
    if(!this.db.objectStoreNames.contains(storeName)){
    this.store = this.db.createObjectStore(storeName, { keyPath: 'key'});
    }


    }
    request.onerror = (e) => {console.log('Can not open indexedDB', e);};

    错误2:

    Failed to exectue ‘transaction’ on ‘IDBDatabase’: One of the specified stores was not found.

    这是因为upgradeneeded事件没有被触发。 
    这里需要注意upgradeneeded事件。首先,根据API,应该在upgradneeded事件的回调函数中调用createObjectStore方法创建store object,不应该在success的回调中,否则会报错。其次,当为open方法传入一个本域没有的数据库名时,会创建相应的数据库,并触发success、upgradeneeded事件,从而创建一个store object。但是,chrome54并不会触发upgradeneeded事件,造成store object不会被创建,后续在store object上创建事务并操作数据时候就会报错。Stackoverflow上提供的解决办法是,在open方法传入第二个参数(与已有version不同,且更大),这样就会触发chrome上的upgradeneeded事件了。不过,每次都需要调用db.version获取当前的版本号。

    另外可能出现的一个错误是:

    Cannot read property ‘createObjectStore’ of undefined

    这是因为indexedDB是异步的,你必须在回调函数中使用createObjectStore方法,即使你把createObjectStore调用写在open函数后面,也无法保证哪个先完成。
    --------------------- 
    作者:柒青衿 
    来源:CSDN 
    原文:https://blog.csdn.net/qiqingjin/article/details/53263261 
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    mysql数据库常用指令
    解决windows的mysql无法启动 服务没有报告任何错误的经验。
    “Can't open file for writing”或“operation not permitted”的解决办法
    启动Apache出现错误Port 80 in use by "Unable to open process" with PID 4!
    如何打开windows的服务services.msc
    常见的HTTP状态码 404 500 301 200
    linux系统常用的重启、关机指令
    (wifi)wifi移植之命令行调试driver和supplicant
    linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
    alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号
  • 原文地址:https://www.cnblogs.com/BlingSun/p/10056680.html
Copyright © 2011-2022 走看看