我正试图在移动(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 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"}
将适配器更改为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
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在哪里存储文件(特别是在应用程序中)以及有关目录的读/写权限的事情。也许我应该在其他地方寻找......欢迎所有帮助。
答案 0 :(得分:5)
所以这里有几点需要注意:
如果只是给它一个没有adapter://
前缀的数据库名称,PouchDB将使用适用于平台的最佳适配器。如果要使用本地存储适配器,这可能是创建数据库的最佳方法,因为无论您运行的是什么浏览器,都可以使用相同的代码;只要它支持WebSQL或IndexedDB, 就可以正常工作。
有一个针对Android here的PhoneGap的开放式问题跟踪支持,已于4天前关闭,确认可以正常运行。不幸的是,我现在无法亲自确认这一点,但它应工作。
在拉取请求670上查看howonlee中的注释,特别是关于在deviceready
回调中包装所有内容:
请注意,我们必须在onDeviceReady()函数中创建和使用db,因为我们使用的websql只有在deviceready事件后才可用。