pouchDB数据库锁定 - 无法开始事务

时间:2017-05-09 14:06:48

标签: javascript angularjs ionic-framework pouchdb web-sql

我有以下服务启动pouchDB数据库。

.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; 
    }); 
  }


}])

不知何故,我的数据库被锁定,当我在控制器中执行findUser功能时,我收到以下错误:

错误

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

这是我的代码。我真的很困惑,因为这之前已经奏效并且没有任何问题。真正有趣的是我的get()函数在与数据库通信时没有错误。

有谁知道为什么会发生这种情况以及我可以做些什么来重新启动数据库或深入研究问题所以这不会再发生?

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应该注意,当我重新启动应用程序时,我的代码完全正常。

0 个答案:

没有答案