indexedDB openCursor事务onsuccess返回空数组

时间:2014-09-30 01:15:22

标签: javascript indexeddb

            req = db.openCursor();
            req.customerData=new Array() //[{a:1}]
            req.onsuccess = function(e) {
                var cursor = e.currentTarget.result;
                if (cursor) {
                    //console.log(cursor.value);
                    e.currentTarget.customerData.push(cursor.value);
                    e.currentTarget.customerData.push("hello?");         
                    cursor.continue()
                }
                else {
                    console.log(e.currentTarget.customerData) //this always correct
                }
            }

     console.log(req.customerData); //outside the onsuccess everything is gone?

console.log(req);

我在chrome控制台中打开对象时可以看到customerData

console.log(req.customerData);

但是当我这样做时它是空的?

new Array()替换为[{a:1}]

console.log(req.customerData);

我可以看到a以及其他对象

然后是agian

console.log(req.customerData[0].a);

工作,其他对象消失了。

如何保存customerData?交易完成后,我尝试了推送数字或文本,但同样的事情。我无法获取数据只在事务期间在console.log()上显示它?

我知道它必须是过去引用的东西,但每个变量我都会消失吗?

在下面添加完整示例,只需在控制台

中输入write()和read()即可
    <script>
        var iDB
        ready=function(){
            var request = indexedDB.open("my-database",1);
            request.onupgradeneeded = function(e) {
                var db = e.currentTarget.result
                var store = db.createObjectStore("store", {keyPath: "muts", autoIncrement:false})
                //store.createIndex("by_submit", "submit", {unique: false})
                console.log('db upgrade', 'v'+db.version)
            }
            request.onerror = function(e) {
                //var db = e.currentTarget.result;
                //db.close()
                console.error('db error ',e)
            }
            request.onsuccess = function(e) {
                var db = e.currentTarget.result
                db.onversionchange = function(e) {
                    db.close()
                    console.log('db changed', 'v'+db.version, 'CLOSED')
                }
                console.log('db setup', 'v'+db.version, 'OK')
            }
            iDB=request
        }

        drop=function(){
            iDB.result.close()
            var req = indexedDB.deleteDatabase(this.iDB.result.name);
            req.onsuccess = function() {console.log("Deleted database successfully")}
            req.onerror = function() {console.log("Couldn't delete database")}
            req.onblocked = function() {console.log("Couldn't delete database due to the operation being blocked")}
        }

        read=function(){
            var db=iDB
                    .result
                    .transaction(["store"], "readwrite").objectStore("store");

            var req = db.openCursor();
            req.iData=new Array();
            req.onsuccess = function(e) {
                var cursor = e.currentTarget.result;
                if (cursor) {
                    e.currentTarget.iData.push(cursor.value);
                    e.currentTarget.iData.push("hello");
                    cursor.continue()
                }
                else {
                    console.log(e.currentTarget.iData)
                }
            }

            console.log(req.iData)

        }

        write=function(){
            var db=document.querySelector('my\-database')
                .iDB
                .result
                .transaction(["store"], "readwrite").objectStore("store");

            var customerData = [
                {muts: "Bill", qty: "1"},
                {muts: "Donna", qty: "1"}
            ]

            for (var i in customerData){db.put(customerData[i])}
        }

        ready()
    </script>

1 个答案:

答案 0 :(得分:2)

一些事情

  • 我建议不要设置IDBRequest对象的自定义属性。改为创建和访问外部作用域中的对象。
  • 无需使用event.currentTarget。 event.target就足够了('this'也是如此,请求对象本身也是如此。)
  • onversionchange已弃用。
  • 由于indexedDB的异步特性,您可能正在尝试将某些内容打印到尚不存在或不再存在的控制台。相反,尝试在事务完成时打印出来。

例如:

function populateArray(openDatabaseHandle, onCompleteCallbackFunction) {
  var transaction = openDatabaseHandle.transaction('store');
  var store = transaction.objectStore('store');
  var myArray = [];
  var request = store.openCursor();
  request.onsuccess = function() {
    var cursor = this.result;
    if(!cursor) return;
    myArray.push(cursor.value);
    cursor.continue();
  };
  transaction.oncomplete = function() {
    onCompleteCallbackFunction(myArray);
  };
}

// An example of calling the above function
var conn = indexedDB.open(...);
conn.onsuccess = function() {
  populateArray(this.result, onPopulated);
};

// An example of a callback function that can be passed to 
// the populateArray function above
function onPopulated(data) {
  console.debug(data);
  data.forEach(function(obj) {
    console.debug('Object: %o', obj);
  });
}