我使用了一个基本的ionic3相机模块应用程序,用于在脱机时将图像信息与图像一起存储在本地存储中,并且当用户上线时,数据应该与在线benchdb数据存储同步,这实际上并没有发生。在cli中运行应用程序时未显示任何错误。
我关注了这些网站:
http://masteringionic.com/blog/2016-12-11-using-pouchdb-with-ionic-part-2/
这是database.ts文件
import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import PouchDB from 'pouchdb';
@Injectable()
export class Database {
private _DB : any;
private success : boolean = true;
private _remoteDB : any;
private _syncOpts : any;
constructor(public http : Http,
public alertCtrl : AlertController)
{
this.initialiseDB();
}
initialiseDB()
{
this._DB = new PouchDB('trees');
this._remoteDB = 'http://172.17.4.161:5984/dendronym';
this._syncOpts = { live : true,
retry : true,
continuous : true };
this._DB.sync(this._remoteDB, this._syncOpts)
.on('change', (info) =>
{
console.log('Handling syncing change');
console.dir(info);
})
.on('paused', (info) =>
{
console.log('Handling syncing pause');
console.dir(info);
})
.on('active', (info) =>
{
console.log('Handling syncing resumption');
console.dir(info);
})
.on('denied', (err) =>
{
console.log('Handling syncing denied');
console.dir(err);
})
.on('complete', (info) =>
{
console.log('Handling syncing complete');
console.dir(info);
})
.on('error', (err)=>
{
console.log('Handling syncing error');
console.dir(err);
});
}
handleSyncing()
{
this._DB.changes({
since : 'now',
live : true,
include_docs : true,
attachments : true
})
.on('change', (change) =>
{
// handle change
console.log('Handling change');
console.dir(change);
})
.on('complete', (info) =>
{
// changes() was canceled
console.log('Changes complete');
console.dir(info);
})
.on('error', (err) =>
{
console.log('Changes error');
console.log(err);
});
}
addTree(species, note, image)
{
var timeStamp = new Date().toISOString(),
base64String = image.substring(23),
tree = {
_id : timeStamp,
species : species,
note : note,
_attachments : {
"species.jpg" : {
content_type : 'image/jpeg',
data : base64String
}
}
};
return new Promise(resolve =>
{
this._DB.put(tree).catch((err) =>
{
console.log('error is: ' + err);
this.success = false;
});
if(this.success)
{
this.handleSyncing();
resolve(true);
}
});
}
updateTree(id, species, note, image, revision)
{
var base64String = image.substring(23),
tree = {
_id : id,
_rev : revision,
species : species,
note : note,
_attachments: {
"species.jpg" : {
content_type : 'image/jpeg',
data : base64String
}
}
};
return new Promise(resolve =>
{
this._DB.put(tree)
.catch((err) =>
{
console.log('error is: ' + err);
this.success = false;
});
if(this.success)
{
this.handleSyncing();
resolve(true);
}
});
}
retrieveTree(id)
{
return new Promise(resolve =>
{
this._DB.get(id, {attachments: true})
.then((doc)=>
{
var item = [],
dataURIPrefix = 'data:image/jpeg;base64,',
attachment;
if(doc._attachments)
{
attachment = doc._attachments["species.jpg"].data;
}
else
{
console.log("we do NOT have attachments");
}
item.push(
{
id : id,
rev : doc._rev,
species : doc.species,
note : doc.note,
image : dataURIPrefix + attachment
});
resolve(item);
})
});
}
retrieveTrees()
{
return new Promise(resolve =>
{
this._DB.allDocs({include_docs: true, descending: true, attachments:
true}, function(err, doc)
{
let k,
items = [],
row = doc.rows;
for(k in row)
{
var item = row[k].doc,
dataURIPrefix = 'data:image/jpeg;base64,',
attachment;
if(item._attachments)
{
attachment = dataURIPrefix + item._attachments["species.jpg"].data;
}
else
{
console.log("we do NOT have attachments");
}
items.push(
{
id : item._id,
rev : item._rev,
species : item.species,
note : item.note,
image : attachment
});
}
resolve(items);
});
});
}
removeTree(id, rev)
{
return new Promise(resolve =>
{
var tree = { _id: id, _rev: rev };
this._DB.remove(tree)
.catch((err) =>
{
console.log('error is: ' + err);
this.success = false;
});
if(this.success)
{
resolve(true);
}
});
}
errorHandler(err)
{
let headsUp = this.alertCtrl.create({
title: 'Heads Up!',
subTitle: err,
buttons: ['Got It!']
});
headsUp.present();
}
}