title: Getting a page of results from IndexedDb using cursor
body: <p>I'm trying to learn how to use IndexedDb and my next step is getting a page of results from a store.  My strategy is to store the last key retrieved for the page in the service and use it to open the cursor using that key as the lower bound on the next request.  Here is the function as I originally defined it:</p>
  
  <pre><code>service.getListPage = function(store, pageSize) {
      pageSize = pageSize || 15;
      var deferred = $q.defer();
      //called on cursor open success event
      var getPage = function (cursorEvent) {
          var page = [];
          var cursor = cursorEvent.target.result;
          if (cursor) {
              for (var i = 0; i &lt; pageSize; i++) {
                  page.push(cursor.value);
                  cursor.continue();
              }
              lastKeyOnPage[store] = cursor.key;
              deferred.resolve(page);
          } else {
              deferred.resolve([]);
          }
      }
      var transaction = service.db.transaction([store], "readonly");
      var objectStore = transaction.objectStore(store);
      var cursor;
      if (lastKeyOnPage.hasOwnProperty(store) &amp;&amp; lastKeyOnPage[store]) {
         cursor = objectStore.openCursor(IDBKeyRange.lowerBound(lastKeyOnPage[store]));
      } else {
         cursor = objectStore.openCursor();
      }
      cursor.onsuccess = getPage;
      return deferred.promise;
  }
  </code></pre>
  
  <p>If I tried to use this function with only one item in the store, I ran into two problems:</p>
  
  <ol>
  <li>The continue function would throw an error (so I used a try/catch, resolving the array of values in the catch block)</li>
  <li>The cursor would return the same value, <code>pageSize</code> times (so I tried checking if the primary key in the current loop iteration matched the last one)</li>
  </ol>
  
  <p>This still isn't working, though.  There are now two items in the store, and if I call this function, it gets the first item, and then throws an error saying the cursor is either iterating or past its end.</p>
  
  <p>Am I missing something about how this is supposed to work?  I would just use getAll, but this is for a Cordova application and that method is not available.  How can I just grab a certain number of results?</p>