我正在尝试在移动(android)应用程序中同时使用上述三个。当我在 firefox (20)、Ubuntu (12.04) 中运行它时,它工作得非常好,但是当我将它加载到手机(或 android 模拟器)中时,它似乎无法存储数据。在 firefox 中运行时,数据存储在 sqlite db 中的用户配置文件目录中,但是使用应用程序?如何告诉 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 APIs 而不是 4.2.2 apis),这就是 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
pouchdb-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("idb://testpath/testdb") 这样的网址不起作用......抱怨元数据。在我脑后的某个地方,我在想应该有一种方法可以告诉 PouchDB 将文件存储在哪里(尤其是在应用程序中)以及有关目录的读/写权限的事情。也许我应该寻找其他地方......欢迎所有帮助。