IndexedBD的一些心得(总结)

来源:互联网 发布:期货从业资格考试 知乎 编辑:程序博客网 时间:2024/04/30 08:08

IndexedBD的一些心得

教程推荐

  • HTML5本地存储——IndexedDB(一:基本使用)
  • html5 web IndexedDB使用详解
  • 前端的数据库:IndexedDB入门
  • MDN使用 IndexedDB

心得

IDB的异步特性

当你尝试从result里面想获取出数据并且另外赋值,请记住,IDB是一个异步asyn的数据交互方式,你每次处理都要在一个回执里面进行才行。
错误示范:

var transaction = db.transaction('myQuestionaire','readwrite');var store = transaction.objectStore('myQuestionaire');var IDB = store.get(that.requestInfo);

正确示范:

var transaction = db.transaction('myQuestionaire','readwrite');var store = transaction.objectStore('myQuestionaire');var request = store.get(that.requestInfo);request.onsuccess = function(){    console.log(request.result)//完全可以访问,还能操作}

尝试读取数据时候报错

Uncaught InvalidStateError: Failed to read the ‘result’ property from ‘IDBRequest’: The request has not finished

参考原文

这个时候你可能没懂各个教程里面的各种var声明,一定要注意作用域的问题。

You need to learn about how to write asynchronous Javascript. Your db variable isn’t defined at the time you access it.

错误示范:

var r = indexedDB.open();var db = null;r.onsuccess = function(event) { db = event.target.result); }

正确示范:

var r = indexedDB.open();r.onsuccess = function(event) {  var db = event.target.result;};

that means db isn’t available outside the scope of the onsuccess function. Stop trying to use it outside its scope or you will just run into the problem you are experiencing.

Put的时候报错

failed to execute ‘put’ on ‘idbobjectstore’ evaluating the object store’s key path did not yield a value

参考原文

储存数据的时候必须要带上object store的key一起储存,或者使用一个key generator({autoIncrement: true})
例如:

var store = db.createObjectStore('my_store', {keyPath: 'key'});store.put({key: 11, value: 33}); // OKstore.put({value: 66}); // throws, since 'key' is not presentvar store = db.createObjectStore('my_store', {keyPath: 'key', autoIncrement: true});store.put({key: 11, value: 33}); // OK, key generator set to 11store.put({value: 66}); // OK, will have auto-generated key 12

执行transaction时报错

Uncaught InvalidStateError: Failed to execute ‘transaction’ on ‘IDBDatabase’: A version change transaction is running

参考原文

The versionchange transaction also allows you to readwrite. You just need to access the transaction created for you within the onupgradeneeded function.

function go() {  var req = indexeddb.open(...);  req.onupgradeneeded = function(event) {    var db = event.target.result;    var os = ...    var transaction = event.target.transaction;// the important part    var addRequest = transaction.objectStore('').index('').add('value');    addRequest.onsuccess = function() {console.log('Success!');};  };}

You are encountering the error because you are trying to start a second transaction while the version change transaction is still running.

这个时候联系到的知识点就时versionchange,这种状态的改变在IDB里面要放到打开数据库的回执的onupgradeneeded函数里面。针对onunpgradeneeded的介绍,摘抄了百度知道里面一个回答,看了就懂了。

IDBOpenDBRequest还有一个类似回调函数句柄——onupgradeneeded。
该句柄在我们请求打开的数据库的版本号和已经存在的数据库版本号不一致的时候调用。

indexedDB.open方法还有第二个可选参数,数据库版本号,数据库创建的时候默认版本号为1,当我们传入的版本号和数据库当前版本号不一致的时候onupgradeneeded就会被调用,当然我们不能试图打开比当前数据库版本低的version.

代码中定义了一个myDB对象,在创建indexedDB request的成功毁掉函数中,把request获取的DB对象赋值给了myDB的db属性,这样就可以使用myDB.db来访问创建的indexedDB了。

用indexedBD的时候要善用onerror来获取错误的信息,这样就知道哪里出错了。

以前做一个webapp碰到的坑

参考之前写过的一篇文章——【Hours】使用indexedDB中遇到的问题。

0 0
原创粉丝点击