PouchDB带有jquery mobile和phonegap

Bal*_*ddy 2 jquery-mobile cordova pouchdb

我试图在移动(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>
Run Code Online (Sandbox Code Playgroud)

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

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');
               ...
Run Code Online (Sandbox Code Playgroud)

在firefox中,控制台日志的输出如下,带有idb adapter - 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"}
Run Code Online (Sandbox Code Playgroud)

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

将websql版本apk加载到模拟器中(使用谷歌API而不是4.2.2 apis)这就是logcat上的内容(与logb相同的idb):

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
Run Code Online (Sandbox Code Playgroud)

pouchdb-nightly中的条目来自我添加到小包js文件中的一些console.log语句.js周围的实际代码如下(必须stringify,因为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]};
  }
Run Code Online (Sandbox Code Playgroud)

非常感谢任何有关解决此问题的方法的帮助/建议.感谢切斯特并经常提出您的建议 - 我现在就去看看他们.仅供参考 - 像Pouch这样的网址("idb:// testpath/testdb")不起作用......抱怨元数据.在我脑后的某个地方,我认为应该有一种方法可以告诉PouchDB在哪里存储文件(特别是在应用程序中)以及有关目录的读/写权限的事情.也许我应该在别的地方寻找...欢迎所有的帮助.

jch*_*hes 5

所以这里有一些注意事项:

  1. 如果您只是给它一个没有adapter://前缀的数据库名称,PouchDB将使用适用于平台的最佳适配器.如果要使用本地存储适配器,这可能是创建数据库的最佳方法,因为无论您运行的是什么浏览器,都可以使用相同的代码; 只要它支持WebSQL或IndexedDB就可以了.

  2. 有一个开放的问题跟踪针对Android的PhoneGap的支持在这里,这是3天前关闭,确认是工作.不幸的是,我现在无法亲自确认这一点,但它应该有效.

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

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