PouchDB with jquery mobile & phonegap



我试图在移动(android)应用程序中一起使用上述三个。当我在firefox (20), Ubuntu(12.04)中运行它时,它运行得很好,但当我将它加载到手机(或android模拟器)时,它似乎无法存储数据。当在firefox中运行时,数据存储在sqlite数据库中的用户配置文件目录中,但是在应用程序中呢?我如何告诉PouchDB在哪里存储数据库?我该怎么做呢?

——附加信息

我一直在做一些更多的测试与PouchDB适配器类型-在firefox和chrome。firefox不支持WebSQL,因此使用chrome进行测试。以下是我在index.html中加载的脚本。

<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.3.1.min.css" />
<script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery-1.9.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile/pouch-20130419/pouchdb-nightly.js"></script>
<script type="text/javascript" charset="utf-8" src="testa.js"></script>

下面是生成这些日志的一些相关代码

console.log("Creating the database" + JSON.stringify(userdata));
Pouch('idb://'+userdata.username, function(err, database){
    if (err) {
    console.log('Failed to create database:'+ JSON.stringify(err));
        return {'ok': false, 'reason':'Failed to create database:'+ userdata.username};
    } else {
        pdb = database;
        console.log('Created database:'+ JSON.stringify(userdata.username));
        Pouch('idb://cred', function(err, credb){
           if (err) {
               console.log('Failed to create database: cred'+ JSON.stringify(err));
               //
               return {'ok': false, 'reason':'Failed to create database: cred'};
           } else {
               pdbcred = credb;
               console.log('Created database: cred');
               ...

在firefox中,控制台日志的输出如下所示,idb适配器- Pouch("idb://idbx")

testa.js (line 296)     Creating the database{"username":"idbx","email":"idbx@asdf.asdf","password":"password"} 
pouchd...htly.js (line 759) idb://idbx 
pouchd...htly.js (line 764) ["idb://idbx","idb","idbx"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"idbx","adapter":"idb"} 
pouchd...htly.js (line 759) idb://_pouch__allDbs 
pouchd...htly.js (line 764) ["idb://_pouch__allDbs","idb","_pouch__allDbs"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"_pouch__allDbs","adapter":"idb"} 
testa.js (line 303)     Created database:"idbx" 
pouchd...htly.js (line 759) idb://cred 
pouchd...htly.js (line 764) ["idb://cred","idb","cred"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"cred","adapter":"idb"} 
pouchd...htly.js (line 759) idb://_pouch__allDbs 
pouchd...htly.js (line 764) ["idb://_pouch__allDbs","idb","_pouch__allDbs"] 
pouchd...htly.js (line 767) idb 
pouchd...htly.js (line 768) {} 
pouchd...htly.js (line 772) {"name":"_pouch__allDbs","adapter":"idb"} 
testa.js (line 311)     Created database: cred 
testa.js (line 328)     Saved document:{"_id":"idbx","password":"password","email":"idbx@asdf.asdf","savecred":true}:{"ok":true,"id":"idbx","rev":"1-2f0ff07a7c0d8fb6f172a6bfe8a9174f"}

将适配器更改为websql并在chrome上测试-工作正常

将websql版本apk加载到模拟器中(使用google api而不是4.2.2 api),这是logcat上出现的结果(与idb logcat相同):

04-19 00:28:26.571: I/Web Console(22733): Creating the database{"username":"websqlo","email":"websqlo@asdf.asdf","password":"password"} at file:///android_asset/www/testa.js:296
04-19 00:28:26.601: I/Web Console(22733): websql://websqlo at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:759
04-19 00:28:26.611: I/Web Console(22733): ["websql://websqlo","websql","websqlo"] at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:764
04-19 00:28:26.631: I/Web Console(22733): "websql" at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:767
04-19 00:28:26.651: I/Web Console(22733): {} at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:768
04-19 00:28:26.751: E/Web Console(22733): Uncaught TypeError: Cannot call method 'valid' of undefined at file:///android_asset/www/jquery.mobile/pouch-20130419/pouchdb-nightly.js:769

来自pouch -nightly的条目来自我添加到pouch js文件中的一些console.log语句。js周围的实际代码如下(必须进行字符串化,因为logcat只显示纯文本):

Pouch.parseAdapter = function(name) {
  console.log(name);
  var match = name.match(/([a-z-]*)://(.*)/);
  var adapter;
  if (match) {
    // the http adapter expects the fully qualified name
    console.log(JSON.stringify(match));
    name = /http(s?)/.test(match[1]) ? match[1] + '://' + match[2] : match[2];
    adapter = match[1];
    console.log(JSON.stringify(adapter));
    console.log(JSON.stringify(Pouch.adapters))
    if (!Pouch.adapters[adapter].valid()) {
      throw 'Invalid adapter';
    }
    console.log(JSON.stringify({name: name, adapter: match[1]}));
    return {name: name, adapter: match[1]};
  }

将感谢任何帮助/建议的方法来解决这个问题…感谢chesles和频繁的你的建议-我会去看看他们现在。仅供参考-像Pouch这样的url("idb://testpath/testdb")不起作用…抱怨元数据。在我脑后的某个地方,我在想应该有一种方法告诉PouchDB在哪里存储文件(特别是在应用程序中),以及关于目录上的读/写权限的事情。也许我应该去别的地方看看……欢迎大家帮忙

这里有几点需要注意:

  1. 如果您只是给它一个数据库名称,不带adapter://前缀,那么PouchDB将使用平台可用的最佳适配器。如果您想使用本地存储适配器,这可能是创建数据库的最佳方式,因为无论您在什么浏览器上运行,都可以使用相同的代码;只要它支持WebSQL或IndexedDB,它应该工作

  2. 有一个公开的问题跟踪支持android的PhoneGap,这是4天前关闭,确认工作。不幸的是,我现在不能亲自确认这一点,但它应该工作。

  3. 查看howonlee对pull request 670的注释,特别是关于将所有内容包装在deviceready回调中:

    请注意,我们必须在onDeviceReady()函数中创建和使用db,因为我们使用的websql只有在deviceready事件之后才可用。

最新更新