【问题标题】:objectStore.openCursor().onsuccess is not getting calledobjectStore.openCursor().onsuccess 没有被调用
【发布时间】:2014-05-28 05:06:45
【问题描述】:

我刚开始使用 indexedDB。

我能够在对象存储中写入数据并在第一次点击时获取数据。 当我尝试再次点击时,我观察到将数据写入 DB 可以正常工作,但似乎无法调用 OpenCursor.Success。

谁能指出差距?

这是我的代码..

function clickMe()
{
alert("Yes am inside the function..");
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;

try {
  var dbOpenRequest = window.indexedDB.open("MyDB");
  alert("congretss I Opened The DB as well..");

  dbOpenRequest.onsuccess = function(event){
  try {
     alert("I am inside the onsuccess Method");
     var db = dbOpenRequest.result;
     db.onerror=function(event){

     }; 

     var transaction = db.transaction(["My_ObjectStore"], "readwrite");

      transaction.oncomplete = function(e){

      };
      transaction.onabort = function(e){

      };
      transaction.onerror = function(e){

      };


      try {
        var objectStore = transaction.objectStore("My_ObjectStore");
       alert("Wait am trying to write the data in DB..");
        var objectId = new Date().getTime()
                    var data =([ {
                    "reference": "<a href='details.html'>DEV2014/557</a>",
                    "applicant": "Association",
                    "siteAddress": "631 Gregory Terrace, Bowen Hills",
                    "applicationType": "Decided Application",
                    "status": "Approved",
                    "action": "<a href='details.html' class='btn btn-primary'>View Details</a>",
                    "id": 1
                    },
                     {
                    "reference": "<a href='details.html'>DEV2014/557</a>",
                    "applicant": "Association",
                    "siteAddress": "631 Gregory Terrace, Bowen Hills",
                    "applicationType": "Decided Application",
                    "status": "Approved",
                    "action": "<a href='details.html' class='btn btn-primary'>View Details</a>",
                    "id": 2
                    }
                    ]);

        for(i in data)
        {
        var request = objectStore.add(data[i]);  
        }
        alert("Writing data is finished.."+request);

        //get all the data using cursor 
        objectStore.openCursor().onsuccess = function(event) {
        alert("Now you can See the Data..");
         var cursor = event.target.result;

         if (cursor) {
            alert(cursor.value.id);

            cursor.continue();
            } 
             else {
            //alert("No more entries!");
                }     
           };   

        }catch(e){alert("error while writting the data");}



    }catch (e){         
            alert("An error occured when creating object store");
            writeError(e);
            }
     };

 dbOpenRequest.onupgradeneeded = function(event){
 try{
     var db = dbOpenRequest.result;
     var transaction = dbOpenRequest.transaction;
     var objectStore = db.createObjectStore("My_ObjectStore",{
        "keyPath": "id",
        "autoIncrement": true
      });
       }catch(e){alert("Error while crating object store");}
    };
  }
catch(e){alert("error");}
}

【问题讨论】:

  • 为什么要在代码周围使用 try/catch 块?它不是必需的。你为什么把这个问题标记为jquery?这与jquery无关。你为什么要记录'congretss I Opened The DB as well..'而实际上你还没有打开数据库?你为什么要在不同的范围内反复定义对 onerror 的回调?为什么将同步 window.alert 与异步代码调用一起使用?为什么你半随机地使用分号来结束你的陈述?为什么要使用 for-in 来遍历数组?为什么你的代码无法阅读?

标签: javascript jquery indexeddb


【解决方案1】:

我观察到将数据写入数据库很顺利

我只是复制/粘贴并测试了您的代码,第一次编写运行良好,但其他代码没有(您可以使用浏览器调试工具查看 indexeddb 内容)。

原因: 您每次都设置相同的主键:

"id": 1
"id": 2

所以我无法真正解释为什么您的打开游标在那之后失败(可能是因为您的对象存储请求处于错误状态?),但这似乎是问题所在......

【讨论】:

  • 谢谢 VIoz,我之前也注意到了。当我将 id 从 1,2 更改为 3,4 并再次点击时,它会显示数据 1,2,3,4!假设说我在 indexDB 中有这些数据我想查看这些数据我怎么能?似乎只有在将某些数据写入数据库时​​才会触发 Open Cursor,否则不会。
  • 你不必写任何东西,在同一个代码中,如果你去掉两个alert之间的写部分,它就可以工作了……
  • 感谢 VIoz,是的,它工作正常。但是如果要获得单独的 HTML 文件来写入数据和读取数据,那么我需要在用户浏览器上运行这两个文件。有没有什么方法可以让用户在调用 reading.html 之前访问 reading.html 文件时,应该触发 writting.html。实际上我希望在 S3 上托管这些文件。现在如果有人点击这个文件,那么 indexedDB 应该首先用数据初始化,然后只有用户才能获取数据。
  • 我建议您为这个问题打开一个新的详细线程,我无法帮助您解决这个问题,因为我没有使用 JS。请将此主题设置为“已回答”。
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多