1
(function () {

    var db;

    function openDB(dbname, dbtable, keypath) {
        var version = 1;

        // open the database
        var indexeddb_request = indexedDB.open( dbname, version ); // connect + open database

        // if error
        indexeddb_request.onerror = function ( event ) {
            console.log( event.target );
            console.trace();
        }

        // if success
        indexeddb_request.onsuccess = function ( event ) {
            console.log( event.target );
            console.trace();
            db = event.target.result;
        }

        // if onupgradeneeded
        indexeddb_request.onupgradeneeded = function( event ) {
            console.log( event.target );
            console.trace();
            db = event.target.result;
            var objectStore = db.createObjectStore( dbtable, { keyPath: 'url' } );
        }

    }

    function getObjectStore(store_name, mode) {
        var tx = db.transaction(store_name, mode);
        return tx.objectStore(store_name);
    }

    function addPublication(dbtable, data) {
        console.log("addPublication arguments:", arguments);

        var store = getObjectStore(dbtable, 'readwrite');
        var req;
        try {
            for (var i in data) {
                req = store.add(data[i]);
            }
          //req = store.add(data);
        } catch (e) {
          if (e.name == 'DataCloneError')
            displayActionFailure("This engine doesn't know how to clone a Blob, " +
                                 "use Firefox");
          throw e;
        }
        req.onsuccess = function (evt) {
          console.log("Insertion in DB successful");
        };
        req.onerror = function() {
          console.error("addPublication error", this.error);
        };
    } 

    function displayPubList(store, store_name) {
        console.log("displayPubList");

        if (typeof store == 'undefined')
          store = getObjectStore(store_name, 'readonly');


        var req;
        req = store.count();
        // Requests are executed in the order in which they were made against the
        // transaction, and their results are returned in the same order.
        // Thus the count text below will be displayed before the actual pub list
        // (not that it is algorithmically important in this case).
        req.onsuccess = function(evt) {
           console.log("Count of records " + evt.target.result +
                         ' record(s) in the object store.');
        };
        req.onerror = function(evt) {
          console.error("add error", this.error);
        };

        var i = 0;
        req = store.openCursor();
        req.onsuccess = function(evt) {
          var cursor = evt.target.result;

          // If the cursor is pointing at something, ask for the data
          if (cursor) {
            console.log("displayPubList cursor:", cursor);
            req = store.get(cursor.key);
            req.onsuccess = function (evt) {
              var value = evt.target.result;
              console.log("Key :" + cursor.key + 'value :' + value.ssn );
            };

            // Move on to the next object in store
            cursor.continue();

            // This counter serves only to create distinct ids
            i++;
          } else {
            console.log("No more entries");
          }
        };
    }

    openDB("abc","names","ssn");

    const customerData = [
      { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
      { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
    ];

    addPublication("names",customerData);

    displayPubList("abc","names");

})();

我正在尝试从 chrome 开发人员工具中测试上述代码。但看起来在某些情况下呼叫会成功或需要升级。很多时候它总是为空。如果我在这里做错了什么,请告诉我。

4

0 回答 0