3

I have the following service that starts a pouchDB database.

.service("$pouchDB", ["$rootScope", "$q", "$cordovaNetwork", function($rootScope, $q, $cordovaNetwork) {

  var self = this;

  self.db = new PouchDB(localStorage['uid'], {auto_compaction: true});

  ////////
  ////////

  self.findUser = function(idnum) {
    return $q.when(self.db.query('filters/users'))
    .then(function(resp) {
      var found = false, found_id;
      angular.forEach(resp.rows, function(u, id) { if(u.value == idnum) { found = true; found_id = u.id; } });
      if(found) return found_id; else return false;
    })
    .catch(function(resp) { console.log(JSON.stringify(resp)); return null; })
  }

  self.get = function(id, attachment_bool) { 
    return $q.when(self.db.get(id, {attachments:attachment_bool}))
    .then(function(data) { 
        return data; 
    }); 
  }


}])

Somehow, my database got locked and I am receiving the following error when I do the findUser function in my controller:

ERROR

{  
  "status":500,
  "name":"web_sql_went_bad",
  "message":"unknown",
  "error":true,
  "reason":"unable to begin transaction (5 database is locked)"
}

Here is my code. I'm really confused because this has worked before and have had no issues. What's really interesting is my get() function gives no errors when communication with the database.

Does anyone know why this might happen and what I can do to either restart the database or dive deeper into the problem so this doesn't happen again?

CODE

.controller('document', function($scope, $rootScope, $http, $state, $stateParams, $cordovaNetwork, $cordovaDialogs, $q, $pouchDB) {

   //THIS GIVES ME ERROR
   $q.when($pouchDB.findUser($rootScope.id)).then(function(user){
     console.log('HI');
    });

   //NO ERROR
   $q.when($pouchDB.get($stateParams.template_id, true))
   .then(function(data) {
     console.log("hello");
   });

IT SHOULD BE NOTED that when I restart my application, my code works perfectly fine.

bryan
  • 8,879
  • 18
  • 83
  • 166

0 Answers0